Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在while循环中声明字符串变量而不在整个过程中循环-java_Java_Loops_While Loop - Fatal编程技术网

在while循环中声明字符串变量而不在整个过程中循环-java

在while循环中声明字符串变量而不在整个过程中循环-java,java,loops,while-loop,Java,Loops,While Loop,我被困在一个应该声明一个名为“phrase”的字符串变量的地方,在这里它不应该一直循环 给你一个想法,我的任务是:类似于选项1,不同的是用户在输入第一组的结果时输入'N'(而不是'Q')。然后,程序输入第二个团队名称及其结果,直到输入“Q”。输出两条语句,如选项1中的语句,然后是第三条语句,说明哪支球队排名第一(基于分数) 样本输入: 2 Toronto W W L O W O W N Montreal // how would I make this appear in the same wh

我被困在一个应该声明一个名为“phrase”的字符串变量的地方,在这里它不应该一直循环

给你一个想法,我的任务是:类似于选项1,不同的是用户在输入第一组的结果时输入'N'(而不是'Q')。然后,程序输入第二个团队名称及其结果,直到输入“Q”。输出两条语句,如选项1中的语句,然后是第三条语句,说明哪支球队排名第一(基于分数)

样本输入:

2
Toronto
W
W
L
O
W
O
W
N
Montreal // how would I make this appear in the same while loop?
L
L
O 
L
L
W
L
L
Q
2
Toronto
W
W
L
O
W
O
W
N
Montreal 
L
L
O 
L
L
W
L
L
Q
样本输出:

Toronto has played 7 games and has earned 10 points
Montreal has played 8 games and has earned 3 points
Toronto is in first place by 7 points
更新

我的代码:

else if (option == 2){
            int counter = 0;
            int totalpoints = 0;
            String phrase = keyboard.next();
            while(go){
                String letter = keyboard.next();
                    if (letter.equals("W")){
                    pointsW++;
                    }
                    else if (letter.equals("L")){
                    pointsL++;
                    }
                    else if (letter.equals("O")){
                    pointsO++;
                    }
                    counter++;
                    if (letter.equals("N")){
                        totalpoints = pointsW + pointsL + pointsO;
                        counter--;
                        go = false;
                }
            }
            int counter2 = 0;
            int totalpoints2 = 0;
            pointsW = 2;
            pointsL = 0;
            pointsO = 1;
            String phrase2 = keyboard.next();
                while (go2){
                    String letter2 = keyboard.next();
                    if (letter2.equals("W")){
                    pointsW++;
                    }
                    else if (letter2.equals("L")){
                    pointsL++;
                    }
                    else if (letter2.equals("O")){
                    pointsO++;
                    }
                    counter2++;
                    if (letter2.equals("Q")){
                        counter2--;
                        totalpoints2 = pointsW + pointsL + pointsO;
                        go2 = false;
                    }
                }
                            System.out.println(phrase + " has played "+counter+" games and has earned "+totalpoints+" points");
                            System.out.println(phrase2 + " has played "+counter2+" games and has earned "+totalpoints2+" points");
                    if (totalpoints > totalpoints2){
                            System.out.println(phrase + " is in first place by "+(totalpoints - totalpoints2) + " points");
                    }else{
                            System.out.println(phrase2 + " is in first place by "+(totalpoints2 - totalpoints) + " points");
            }
        }
样本输入:

2
Toronto
W
W
L
O
W
O
W
N
Montreal // how would I make this appear in the same while loop?
L
L
O 
L
L
W
L
L
Q
2
Toronto
W
W
L
O
W
O
W
N
Montreal 
L
L
O 
L
L
W
L
L
Q
问题:这是我得到的输出“蒙特利尔打了8场比赛,得了11分”,而应该是“蒙特利尔打了8场比赛,得了3分”



您可以将此代码用于选项二

     Scanner keyboard = new Scanner(System.in);

    int teamCounter = 1;
    //String[] teamsNames = new String[2];
    String teamOneName="";
    String teamTwoName="";
    //int[] playedGames = new int[2];
    int playedGamesTeamOne = 0;
    int playedGamesTeamTwo = 0;
    //int[] points = new int[2];
    int teamOnePoints = 0;
    int teamTwoPoints = 0;
    boolean firstTimeTeam1 = true;
    boolean firstTimeTeam2 = true;
    while (teamCounter <= 2) {
        if (teamCounter == 1) {
            if (firstTimeTeam1) {
                teamOneName = keyboard.nextLine();
                firstTimeTeam1 = false;
            }

            String letter = keyboard.next();
            if (letter.equals("W")) {
                teamOnePoints += 2;
                playedGamesTeamOne++;
            } else if (letter.equals("L")) {
                playedGamesTeamOne++;
            } else if (letter.equals("O")) {
                teamOnePoints += 1;
                playedGamesTeamOne++;
            } else if (letter.equals("N")) {
                teamCounter++;
            }


        } else {
            if (firstTimeTeam2) {
                teamTwoName = keyboard.next();
                firstTimeTeam2 = false;
            }

            String letter = keyboard.next();
            if (letter.equals("W")) {
                teamTwoPoints += 2;
                playedGamesTeamTwo++;
            } else if (letter.equals("L")) {
                playedGamesTeamTwo++;
            } else if (letter.equals("O")) {
                teamTwoPoints += 1;
                playedGamesTeamTwo++;
            } else if (letter.equals("Q")) {
                teamCounter++;
            }
        }
    }
    System.out.println(teamOneName + " has played " + playedGamesTeamOne + " games and has earned " + teamOnePoints + " points");
    System.out.println(teamTwoName + " has played " + playedGamesTeamTwo + " games and has earned " + teamTwoPoints + " points");
    if (teamOnePoints > teamTwoPoints) {
        System.out.println(teamOneName + " is in first place by " + (teamOnePoints-teamTwoPoints) + " points");
    } else {
        System.out.println(teamTwoName + " is in first place by " + (teamTwoPoints-teamOnePoints) + " points");
    }
扫描仪键盘=新扫描仪(System.in);
int团队计数器=1;
//字符串[]teamsNames=新字符串[2];
字符串teamOneName=“”;
字符串teamTwoName=“”;
//int[]playedGames=新int[2];
int playedGamesTeamOne=0;
int playedGameStreattwo=0;
//int[]点=新的int[2];
int teamOnePoints=0;
int teamtwoints=0;
布尔值firstTimeTeam1=true;
布尔值firstTimeTeam2=true;
while(团队计数器团队两点){
System.out.println(teamOneName+)以“+(teamOnePoints teamtwoints)+”点”排在第一位;
}否则{
System.out.println(teamTwoName+)以“+(teamTwoPoints teamOnePoints)+”点”排在第一位;
}

您可以将此代码用于选项二

     Scanner keyboard = new Scanner(System.in);

    int teamCounter = 1;
    //String[] teamsNames = new String[2];
    String teamOneName="";
    String teamTwoName="";
    //int[] playedGames = new int[2];
    int playedGamesTeamOne = 0;
    int playedGamesTeamTwo = 0;
    //int[] points = new int[2];
    int teamOnePoints = 0;
    int teamTwoPoints = 0;
    boolean firstTimeTeam1 = true;
    boolean firstTimeTeam2 = true;
    while (teamCounter <= 2) {
        if (teamCounter == 1) {
            if (firstTimeTeam1) {
                teamOneName = keyboard.nextLine();
                firstTimeTeam1 = false;
            }

            String letter = keyboard.next();
            if (letter.equals("W")) {
                teamOnePoints += 2;
                playedGamesTeamOne++;
            } else if (letter.equals("L")) {
                playedGamesTeamOne++;
            } else if (letter.equals("O")) {
                teamOnePoints += 1;
                playedGamesTeamOne++;
            } else if (letter.equals("N")) {
                teamCounter++;
            }


        } else {
            if (firstTimeTeam2) {
                teamTwoName = keyboard.next();
                firstTimeTeam2 = false;
            }

            String letter = keyboard.next();
            if (letter.equals("W")) {
                teamTwoPoints += 2;
                playedGamesTeamTwo++;
            } else if (letter.equals("L")) {
                playedGamesTeamTwo++;
            } else if (letter.equals("O")) {
                teamTwoPoints += 1;
                playedGamesTeamTwo++;
            } else if (letter.equals("Q")) {
                teamCounter++;
            }
        }
    }
    System.out.println(teamOneName + " has played " + playedGamesTeamOne + " games and has earned " + teamOnePoints + " points");
    System.out.println(teamTwoName + " has played " + playedGamesTeamTwo + " games and has earned " + teamTwoPoints + " points");
    if (teamOnePoints > teamTwoPoints) {
        System.out.println(teamOneName + " is in first place by " + (teamOnePoints-teamTwoPoints) + " points");
    } else {
        System.out.println(teamTwoName + " is in first place by " + (teamTwoPoints-teamOnePoints) + " points");
    }
扫描仪键盘=新扫描仪(System.in);
int团队计数器=1;
//字符串[]teamsNames=新字符串[2];
字符串teamOneName=“”;
字符串teamTwoName=“”;
//int[]playedGames=新int[2];
int playedGamesTeamOne=0;
int playedGameStreattwo=0;
//int[]点=新的int[2];
int teamOnePoints=0;
int teamtwoints=0;
布尔值firstTimeTeam1=true;
布尔值firstTimeTeam2=true;
while(团队计数器团队两点){
System.out.println(teamOneName+)以“+(teamOnePoints teamtwoints)+”点”排在第一位;
}否则{
System.out.println(teamTwoName+)以“+(teamTwoPoints teamOnePoints)+”点”排在第一位;
}
  • 您可以对单个点重复使用相同的变量,即
    pointsW
    pointsO
    ,因为您不希望在发布结果之前保留它们的值。循环条件变量(即
    go
    )和用于输入赢/输的变量(即
    letter
    )的情况也是如此
  • 您需要数组或不同的变量来存储总分、计数和团队名称

    import java.util.Scanner;
    
    public class Standings {
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            int option = keyboard.nextInt();
            int pointsW = 0;
            int pointsO = 0;
            String letter;
            boolean go = true;
            if (option == 2) {
                // Variables for total points, counting, and name for the first team
                int playedGamesTeamOne = 0;
                int teamOnePoints = 0;
                String teamOneName = keyboard.next();
                while (go) {
                    letter = keyboard.next();
                    if (letter.equals("W")) {
                        pointsW += 2;
                    } else if (letter.equals("O")) {
                        pointsO++;
                    }
                    playedGamesTeamOne++;
                    if (letter.equals("N")) {
                        teamOnePoints = pointsW + pointsO;
                        playedGamesTeamOne--;
                        go = false;
                    }
                }
    
                // Reset common variables
                go = true;
                pointsW = 0;
                pointsO = 0;
    
                // Variables for total points, counting, and name for the second team
                int playedGamesTeamTwo = 0;
                int teamTwoPoints = 0;
                String teamTwoName = keyboard.next();
                while (go) {
                    letter = keyboard.next();
                    if (letter.equals("W")) {
                        pointsW += 2;
                    } else if (letter.equals("O")) {
                        pointsO++;
                    }
                    playedGamesTeamTwo++;
                    if (letter.equals("Q")) {
                        teamTwoPoints = pointsW + pointsO;
                        playedGamesTeamTwo--;
                        go = false;
                    }
                }
    
                System.out.println(teamOneName + " has played " + playedGamesTeamOne + " games and has earned "
                        + teamOnePoints + " points");
                System.out.println(teamTwoName + " has played " + playedGamesTeamTwo + " games and has earned "
                        + teamTwoPoints + " points");
                if (teamOnePoints > teamTwoPoints) {
                    System.out
                            .println(teamOneName + " is in first place by " + (teamOnePoints - teamTwoPoints) + " points");
                } else {
                    System.out
                            .println(teamTwoName + " is in first place by " + (teamTwoPoints - teamOnePoints) + " points");
                }
            }
        }
    }
    
    运行示例:

    2
    Toronto
    W
    W
    L
    O
    W
    O
    W
    N
    Montreal 
    L
    L
    O
    L
    L
    W
    L
    L
    Q
    Toronto has played 7 games and has earned 10 points
    Montreal has played 8 games and has earned 3 points
    Toronto is in first place by 7 points
    
  • 您可以对单个点重复使用相同的变量,即
    pointsW
    pointsO
    ,因为您不希望在发布结果之前保留它们的值。循环条件变量(即
    go
    )和用于输入赢/输的变量(即
    letter
    )的情况也是如此
  • 您需要数组或不同的变量来存储总分、计数和团队名称

    import java.util.Scanner;
    
    public class Standings {
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            int option = keyboard.nextInt();
            int pointsW = 0;
            int pointsO = 0;
            String letter;
            boolean go = true;
            if (option == 2) {
                // Variables for total points, counting, and name for the first team
                int playedGamesTeamOne = 0;
                int teamOnePoints = 0;
                String teamOneName = keyboard.next();
                while (go) {
                    letter = keyboard.next();
                    if (letter.equals("W")) {
                        pointsW += 2;
                    } else if (letter.equals("O")) {
                        pointsO++;
                    }
                    playedGamesTeamOne++;
                    if (letter.equals("N")) {
                        teamOnePoints = pointsW + pointsO;
                        playedGamesTeamOne--;
                        go = false;
                    }
                }
    
                // Reset common variables
                go = true;
                pointsW = 0;
                pointsO = 0;
    
                // Variables for total points, counting, and name for the second team
                int playedGamesTeamTwo = 0;
                int teamTwoPoints = 0;
                String teamTwoName = keyboard.next();
                while (go) {
                    letter = keyboard.next();
                    if (letter.equals("W")) {
                        pointsW += 2;
                    } else if (letter.equals("O")) {
                        pointsO++;
                    }
                    playedGamesTeamTwo++;
                    if (letter.equals("Q")) {
                        teamTwoPoints = pointsW + pointsO;
                        playedGamesTeamTwo--;
                        go = false;
                    }
                }
    
                System.out.println(teamOneName + " has played " + playedGamesTeamOne + " games and has earned "
                        + teamOnePoints + " points");
                System.out.println(teamTwoName + " has played " + playedGamesTeamTwo + " games and has earned "
                        + teamTwoPoints + " points");
                if (teamOnePoints > teamTwoPoints) {
                    System.out
                            .println(teamOneName + " is in first place by " + (teamOnePoints - teamTwoPoints) + " points");
                } else {
                    System.out
                            .println(teamTwoName + " is in first place by " + (teamTwoPoints - teamOnePoints) + " points");
                }
            }
        }
    }
    
    运行示例:

    2
    Toronto
    W
    W
    L
    O
    W
    O
    W
    N
    Montreal 
    L
    L
    O
    L
    L
    W
    L
    L
    Q
    Toronto has played 7 games and has earned 10 points
    Montreal has played 8 games and has earned 3 points
    Toronto is in first place by 7 points
    

  • 我编辑了我的答案,使其在没有阵列的情况下工作,请检查。我编辑了我的答案,使其在没有阵列的情况下工作,请检查。我用选项2的新代码编辑了我的帖子,我的输出显示了玩家每赢一场比赛应该得到多少分(
    W
    )?玩家每次损失(
    L
    )应该损失多少分?每个
    O
    ,玩家应该输/赢多少分?
    O
    具体说明了什么?那么“W”表示2分,“L”表示0分,“O”表示玩家在“蒙特利尔”场景中得到的1分,应该是“蒙特利尔赢3分”,但我得到的是“蒙特利尔赢11分”,这是为什么?那么一场胜利值2分,一场损失值0分,加时赛值1分。因此,由于蒙特利尔有6次失利,1次获胜,1次失利,总得分将为0+0+0+0+0+0+0+2+1=3分,我用选项2的新代码编辑了我的帖子,我的输出显示了玩家每赢一次应该得到多少分(
    W
    )?玩家每次损失(
    L
    )应该损失多少分?每个
    O
    ,玩家应该输/赢多少分?
    O
    具体说明了什么?那么“W”表示2分,“L”表示0分,“O”表示玩家在“蒙特利尔”场景中得到的1分,应该是“蒙特利尔赢3分”,但我得到的是“蒙特利尔赢11分”,这是为什么?那么一场胜利值2分,一场损失值0分,加时赛值1分。因此,由于蒙特利尔有6次失利,1次获胜,1次失利,因此总体得分将为0+0+0+0+0+0+2+1=3分