小编典典

在while循环中声明字符串变量,而不会在整个循环中循环-Java

java

我被困在我应该声明一个称为“ 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

样本输出:

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 
L
L
O 
L
L
W
L
L
Q

问题 :这是我得到的输出“蒙特利尔参加了8场比赛并获得11分”,而应该是“蒙特利尔参加了8场比赛并获得3分”

我得到的输出



阅读 332

收藏
2020-11-26

共1个答案

小编典典

  1. 您可以对单个点重复使用相同的变量,即pointsWpointsO因为您不想保留它们的值,直到发布结果的最后。循环条件变量(即)go和用于输入获胜/亏损的变量(即)的情况相同letter
  2. 您将需要数组或其他变量来存储总分,计数和小组名称。
        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
2020-11-26