我将问题发生点与解决办法写在注解里....
你自己去看看吧.....
复制程式
public class E9430068{
public static void main (String[] args){
double week [] []={{18.2,17.3,15.0,13.4,0},
{23.8,25.1,20.6,17.8,0},
{20.6,21.5,18.4,15.7,0}}; //所输入的质,后面之所以会多各0是为了补足AVG/4的不足
String n[] ={"早上","中午","晚上"};
System.out.println("未排序");
System.out.println("时段"+"\t"+"星期一"+"\t"+"星期二"+"\t"+"星期三"+"\t"+"星期四"+"\t"+"平均气温");
System.out.println("------"+"\t"+"------"+"\t"+"------"+"\t"+"------"+"\t"+"------"+"\t"+"--------");
for(int i=0;i<week.length;i++){
double avg=0; //先宣告平均数
System.out.print(n[ i ]+"\t");
for(int j=0;j<week[ i ].length-1;j++){ //因为前面有字串有补0,所以要减回来
System.out.print(week[ i ][j]+"\t");
avg+=week[ i ][j]; //宣告平均数的加总
}
week[ i ][4]=avg/4;
System.out.println(week[ i ][4]); //avg/ 后面为天数
}
do //要加一个回圈...没加的话则泡沫排序只排序一次
{
for (int k=week.length-1;k>=0;k--)
{
for (int l=0;l<=week.length-1;l++) //这边有错....k需改成week.length-1
{
if (week[k][l] > week[k][l+1])//当原数值大于后数值时
{ //进行交换
//这边也有错....k+1需改成l+1
double temp=week[k][l];
week[k][l] = week[k][l+1];
week[k][l+1] = temp;
}
}
}
}while(week[0][0] > week[0][1]);//由于你是main函式包含所有功能,
//所以无法使用递回(recursive),只好用这个有点偷吃步的方式解决了...
System.out.println(); //此为空行
System.out.println("排序后");
System.out.println("时段"+"\t"+"星期一"+"\t"+"星期二"+"\t"+"星期三"+"\t"+"星期四"+"\t"+"平均气温");
System.out.println("------"+"\t"+"------"+"\t"+"------"+"\t"+"------"+"\t"+"------"+"\t"+"--------");
for (int k=0;k<week.length;k++){
System.out.print(n[k]+"\t");
for (int l=0;l<week[k].length;l++)
System.out.print(week[k][l]+"\t");
System.out.println();
}
}
}