Scanner scan = new Scanner(System.in);
double [][] DailyTemperature;
double totalTracker = 0.0;
String [] daysofweek = { "Monday ","Tuesday ","Wednsday ","Thursday","Friday ","Saturday ","Sunday "};
DailyTemperature = new double[4][7];
for ( int i = 0; i < DailyTemperature.length; i++ )
for ( int j = 0; j < DailyTemperature[0].length; j++ )
{
System.out.print( "Enter " + daysofweek[j]+ "Temperature for week" + ( i + 1 ) + "\t");
DailyTemperature[i][j] = scan.nextDouble( );
}
所以这个问题只是确认了如何填充2D数组,我的问题是在嵌套for循环中,因为我增加到1它是否保持为1直到j被计数7次然后递增1,或者它们是否同时递增?
答案 0 :(得分:1)
将它们视为堆栈。从外部循环开始,你把它放在堆栈中执行然后进入内部,这样最深层次的循环将位于顶部。你必须完成最顶层元素或最深层循环的工作然后它在它下面循环。这只是这种情况的类比。希望你明白了。
答案 1 :(得分:0)
作为j的内部循环继续正好运行多次,直到达到满足&#39; j&lt; DailyTemperature [0]。长度&#39;然后另一个循环继续进行进一步的迭代。
for ( int i = 0; i < DailyTemperature.length; i++ )//executes firstly once
for ( int j = 0; j < DailyTemperature[0].length; j++ )// continues execution until j < DailyTemperature[0].length.
{
System.out.print( "Enter " + daysofweek[j]+ "Temperature for week" + ( i + 1 ) + "\t");
DailyTemperature[i][j] = scan.nextDouble( );
}
答案 2 :(得分:0)
它是否保持为1,直到j被计数7次然后递增1
是的,但更确切地说,它从0开始,因此它保持为0,然后递增1。
事实上,您可以通过在提示输入之前放置此println语句来轻松验证这一点:
System.out.println("i is now " + i + ", and j is now " + j);
输出:
i is now 0, and j is now 0
Enter Monday Temperature for week1 100
i is now 0, and j is now 1
Enter Tuesday Temperature for week1 200
i is now 0, and j is now 2
Enter Wednsday Temperature for week1 300
.
.
.
i is now 0, and j is now 6
Enter Sunday Temperature for week1 700
i is now 1, and j is now 0
Enter Monday Temperature for week2 800
i is now 1, and j is now 1
Enter Tuesday Temperature for week2