java中while和for循环到无穷大

java中while和for循环到无穷大,java,Java,我有一个Java的练习要做。我需要创建一个世界杯锦标赛,限制是该程序将重新启动,直到我最喜欢的球队获胜。然而,如果我的球队在20次之后没有获胜,那么这个项目就停止了 问题是,当我尝试将第二个限制设置为最大值的20倍,并在为true之后设置一个for时,我总是得到一个无限循环 //Q1 String[] teams16 = {"Uruguay", "Portugal", "France", "Argentina", "Brazil", "Mexico", "Belgium", "Jap

我有一个Java的练习要做。我需要创建一个世界杯锦标赛,限制是该程序将重新启动,直到我最喜欢的球队获胜。然而,如果我的球队在20次之后没有获胜,那么这个项目就停止了

问题是,当我尝试将第二个限制设置为最大值的20倍,并在为true之后设置一个for时,我总是得到一个无限循环

//Q1

String[] teams16 = {"Uruguay", "Portugal", "France", "Argentina", "Brazil", "Mexico", 
    "Belgium", "Japan", "Spain", "Russia", "Croatia", "Denmark", "Sweden", "Switzerland", 
    "Colombia", "England"};


//data
Scanner keyboard = new Scanner(System.in);
Random result = new Random();
System.out.print("Enter your favourite team: ");
String team = keyboard.nextLine();
boolean teamwc = false;

// choice of the favourite team
for (int i = 0 ; i < 16 ; i++ ) {
    if (teams16[i].equalsIgnoreCase(team)) {
        teamwc = true;      
    }
}
if(teamwc == false) {
    System.out.println("Your team is not in the Round of 16 ");
}


// the tournament begins (ROUND OF 16)
while (true) {
    if (teamwc == true) {
        int z = 0;
        String[] winnerof16 = new String[8];
        int a = 0;

        System.out.print("ROUND OF 16:");
        for (int i = 0; i < 16 ; i+=2) {
            int score1 = result.nextInt(5);
            int score2 = result.nextInt(5);

            if (score1 > score2) {
                winnerof16 [a] = teams16[i];
            }
            else if (score1 < score2) {
                winnerof16[a] = teams16[i+1];

            } else if (score1 == score2) {
                Random overtime = new Random();
                int ot = overtime.nextInt(2);
                    if (ot == 0) {
                        score1++;
                        winnerof16[a] = teams16[i];
                    } else if (ot == 1) {
                        score2++;
                        winnerof16[a]=teams16[i+1];
                    }
                }
                System.out.print("["+teams16[i] +"]"+ " " + score1+":"+score2 + " " + "["+teams16[i+1]+"]" + " ");
                a++;    
            }
            System.out.println();
            String[] winnerof8 = new String[4];
            int b = 0;
            System.out.print("QUARTER-FINALS:");
            for (int k = 0 ; k < 8 ; k+=2) { 
                int score3 = result.nextInt(5);
                int score4 = result.nextInt(5);
                if (score3 > score4) {
                    winnerof8[b]=winnerof16[k];
                }
                else if (score3 < score4) {
                    winnerof8[b] = winnerof16[k+1];                 
                } else if (score3 == score4) {
                    Random overtime2 = new Random();
                    int ot2 = overtime2.nextInt(2);
                    if (ot2 == 0) {
                        score3++;
                        winnerof8[b]=winnerof16[k];
                    } else if (ot2 == 1) {
                        score4++;
                        winnerof8[b]=winnerof16[k+1];
                    }                   
                }
                System.out.print("["+winnerof16[k] +"]"+ " " + score3+":"+score4 + " " + "["+winnerof16[k+1]+"]" + " ");
                b++;
            }
            System.out.println();
            String[] winnerof4 = new String[2];
            int c = 0;
            System.out.print("SEMI-FINALS:");
            for (int l = 0 ; l < 4 ; l+=2) { 
                int score5 = result.nextInt(5);
                int score6 = result.nextInt(5);
                if (score5 > score6) {
                    winnerof4[c]=winnerof8[l];
                }
                else if (score5 < score6) {
                    winnerof4[c] = winnerof8[l+1];                  
                } else if (score5 == score6) {
                    Random overtime3 = new Random();
                    int ot3 = overtime3.nextInt(2);
                    if (ot3 == 0) {
                        score5++;
                        winnerof4[c]=winnerof8[l];
                    } else if (ot3 == 1) {
                        score6++;
                        winnerof4[c]=winnerof8[l+1];
                    }                   
                }
                System.out.print("["+winnerof8[l] +"]"+ " " + score5+":"+score6 + " " + "["+winnerof8[l+1]+"]" + " ");
                    c++;
                }
                System.out.println();
                String[] winnerof2 = new String[1];
                int d = 0;
                System.out.print("FINALS:");
                for (int m = 0 ; m < 2 ; m+=2) { 
                    int score7 = result.nextInt(5);
                    int score8 = result.nextInt(5);
                    if (score7 > score8) {
                        winnerof2[d]=winnerof4[m];
                    }
                    else if (score7 < score8) {
                        winnerof2[d] = winnerof4[m+1];                      
                    } else if (score7 == score8) {
                        Random overtime4 = new Random();
                        int ot4 = overtime4.nextInt(2);
                        if (ot4 == 0) {
                            score7++;
                            winnerof2[d]=winnerof4[m];
                        } else if (ot4 == 1) {
                            score8++;
                            winnerof2[d]=winnerof4[m+1];
                        }

                    }
                    System.out.print("["+winnerof4[m] +"]"+ " " + score7+":"+score8 + " " + "["+winnerof4[m+1]+"]" + " ");
                    System.out.println();
                }


                System.out.println("Tournament: " + z +  " The WINNER is: " + winnerof2[d]);
                z++;
                if (winnerof2[d].equalsIgnoreCase(team)) {
                    break;
                }
            }
        }
    }
}
这是我设置第二个限制之前的代码。
我的代码有问题吗?第二个限制我该怎么做?谢谢你

无限循环是由以下两个原因造成的:

while (true) { // it will quit while loop only when an exception raised
if (teamwc == true) { // after this you nowhere assign teamwc == false therefore it is always true
使用布尔变量或条件退出while循环,而不是直接将True赋值给while循环:

t = true
while(t):


您好,您应该编辑您的问题的标题,因为它太宽了。首先,这是家庭作业,其次,你没有提供任何输入或输出的程序。第三,您可能正在使用eclipse进行开发:有用于调试和分步执行、断点等的工具。您可以在google上找到大量文档来帮助您使用它。
while n>0: