英文:
Why does the second while loop not get accessed at all after the first loop?
问题
我创建了一个嵌套的while循环,如图片所示,然而,在第一个完整循环完成之后,尽管第二个while循环内部的条件仍然为true,但第二个while循环并未执行。这可能与System.in
有关,但我尝试了将其注释掉,仍然没有再次循环。这里可能出了什么问题?
int i = 0;
int j = 0;
int p = 0;
while(i < nStudents) {
p = i+1;
System.out.println("\n学生 " + p + " 的排名:");
while(j < nSchools) {
System.out.print("\n学校 " + toArrayList.get(j).getName() + ":");
String ranking = DisplayMenu.br.readLine();
Array1.add(ranking);
j++;
}
i++;
}
我还附加了一个图片,显示了这里实际发生的情况。第二次循环时,它应该打印出学校,但它没有打印出来。
英文:
I created a nested while loop as shown in the image, however, after the first full loop is finished, the second while loop does not execute even though the condition inside the second while loop is still true. This could be an issue with System.in
, but I tried commenting it out and it still did not loop again. What could be the issue here?
int i = 0;
int j = 0;
int p = 0;
while(i < nStudents) {
p = i+1;
System.out.println("\nStudent " + p + "'s rankings: ");
while(j < nSchools) {
System.out.print("\nSchool " + toArrayList.get(j).getName() + ": ");
String ranking = DisplayMenu.br.readLine();
Array1.add(ranking);
j++;
}
i++;
}
I have also included an image, which shows what exactly happens here. For the second time looping through, it should print out the schools but it does not.
答案1
得分: 0
你需要在退出循环后将变量 j 设置为 0。
int i = 0;
int j = 0;
int p = 0;
int nStudents = 10;
int nSchools = 4;
while (i < nStudents) {
p = i + 1;
System.out.println("\n 学生 " + p + " 的排名: ");
while (j < nSchools) {
System.out.print("\n 学校 " + j + " :");
j++;
}
j = 0; //// 像这样
i++;
}
英文:
You need to set variable j to 0 after exiting the loop.
int i = 0;
int j = 0;
int p = 0;
int nStudents = 10;
int nSchools = 4;
while (i < nStudents) {
p = i + 1;
System.out.println("\n Student " + p + "'s rankings: ");
while(j<nSchools){
System.out.print("\n School " + j+ " :");
j++;
}
j=0; //// LIKE THIS
i++;
}
答案2
得分: 0
这不起作用的原因是,一旦第一个完整循环完成(正如您所提到的),由于您编写的增量 (j++;)
,j
变得大于 nStudents
。它永远不会被设置回到0
或低于nStudents
,以便第二次进入while
循环。
int i = 0;
int j = 0;
int p = 0;
while (i < nStudents) {
p = i + 1;
System.out.println("\n学生 " + p + " 的排名:");
while (j < nSchools) {
System.out.print("\n学校 " + toArrayList.get(j).getName() + ":");
String ranking = DisplayMenu.br.readLine();
Array1.add(ranking);
j++;
}
// 在此处将 j 设置为小于 nSchools。一个示例是 j = 0;
j = 0;
i++;
}
英文:
The reason why this does not work is that, once the first full loop is finished (like you mentioned), j
becomes greater than nStudents
because of the increment you wrote (j++;)
. It never gets set back to 0
or lower than nStudents
for the second time it goes back into the while
loop.
int i = 0;
int j = 0;
int p = 0;
while(i < nStudents) {
p = i+1;
System.out.println("\nStudent " + p + "'s rankings: ");
while(j < nSchools) {
System.out.print("\nSchool " + toArrayList.get(j).getName() + ": ");
String ranking = DisplayMenu.br.readLine();
Array1.add(ranking);
j++;
}
//Set j less that nSchools here. One example is j = 0;
i++;
}
答案3
得分: 0
for循环也可以使用
int i = 0;
int nSchools = 4;
for (int nStudents = 10; nStudents > i; i++) {
System.out.println("\n 学生 " + i + " 的排名: ");
for (int j = 0; j < nSchools; j++) {
System.out.print("\n 学校 " + j + " :");
}
i++;
}
英文:
For loop can be used as well
int i = 0;
int nSchools = 4;
for(int nStudents = 10; nStudents > i; i++){
System.out.println("\n Student " + i + "'s rankings: ");
for(int j = 0; j < nSchools; j++){
System.out.print("\n School " + j+ " :");
}
i++;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论