为什么第二个 while 循环在第一个循环后就完全没有被访问到呢?

huangapple go评论65阅读模式
英文:

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 &lt; nStudents) {
			p = i+1;
			System.out.println(&quot;\nStudent &quot; + p + &quot;&#39;s rankings: &quot;);
			
			while(j &lt; nSchools) {
				
				System.out.print(&quot;\nSchool &quot; + toArrayList.get(j).getName() + &quot;: &quot;);
				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 &lt; nStudents) {
            p = i + 1;
            System.out.println(&quot;\n Student &quot; + p + &quot;&#39;s rankings: &quot;);
            while(j&lt;nSchools){
                System.out.print(&quot;\n School &quot; + j+ &quot; :&quot;);                   
                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 &lt; nStudents) {
    p = i+1;
    System.out.println(&quot;\nStudent &quot; + p + &quot;&#39;s rankings: &quot;);
        
    while(j &lt; nSchools) {
            
        System.out.print(&quot;\nSchool &quot; + toArrayList.get(j).getName() + &quot;: &quot;);
        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 &gt; i; i++){
            System.out.println(&quot;\n Student &quot; + i + &quot;&#39;s rankings: &quot;);
            for(int j = 0; j &lt; nSchools; j++){
                System.out.print(&quot;\n School &quot; + j+ &quot; :&quot;);
            }
            i++;
        }

huangapple
  • 本文由 发表于 2020年10月19日 06:33:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64418976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定