【问题标题】:How to end the while loop with either player1 or player2 entering "Q"?如何在 player1 和 player2 输入“Q”时结束 while 循环?
【发布时间】:2020-06-18 17:46:58
【问题描述】:

我被困在游戏中的某个部分,我使用while循环并结束循环并获得游戏结果,我希望“player1”或“player2”输入“Q”,所以我尝试这样做:

if (player1.equals("Q") || player2.equals("Q")){
    go = false; //go is a boolean variable 
}

这似乎不起作用,因为我必须为 player1 和 player2 输入“Q”才能结束游戏,但我只希望 其中一个输入“Q”并且游戏将停止。

代码:

import java.util.Scanner;

public class Team {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Soccer Game Between 2 Teams");
        System.out.println("Win is 2 points" + "\n" + "Loss is worth 0 points" + "\n" + "Overtime is worth 1 point");
        System.out.println("Type W, O, or L" + "\n" + "Type Q to end the game");
        int pointsw = 0;
        int pointsl = 0;
        int pointso = 0;
        int pointsw2 = 0;
        int pointsl2 = 0;
        int pointso2 = 0;
        int totalpoints = 0;
        int totalpoints2 = 0;
        int counter = 0;
        int counter2 = 0;
        boolean go = true;
        System.out.println("\n" + "Enter team one:");
        String phrase = keyboard.next();
        System.out.println("\n" + "Enter team two:");
        String phrase2 = keyboard.next();
        System.out.println();
        while (go) {
            System.out.println("Enter " + phrase + " Result:");
            String team1 = keyboard.next();
            System.out.println("Enter " + phrase2 + " Result");
            String team2 = keyboard.next();
            if (team1.equals("W") || team1.equals("w")) {
                pointsw += 2;
            } else if (team1.equals("O") || team1.equals("o")) {
                pointso += 1;
            } else if (team1.equals("L") || team1.equals("l")) {
                pointsl += 0;
            }
            counter++;
            if (team2.equals("W") || team2.equals("w")) {
                pointsw2 += 2;
            } else if (team2.equals("O") || team2.equals("o")) {
                pointso2 += 1;
            } else if (team2.equals("L") || team2.equals("l")) {
                pointsl2 += 0;
            }
            counter2++;
            totalpoints = pointsw + pointso + pointsl;
            totalpoints2 = pointsw2 + pointso2 + pointsl2;
            if (team1.equals("Q") || team2.equals("Q")) {
                go = false;
                if (totalpoints > totalpoints2) {
                    System.out.println(phrase + " wins with " + totalpoints + " points");
                    System.out.println("It took " + phrase + " " + counter + " rounds to win");
                } else if (totalpoints < totalpoints2) {
                    System.out.println(phrase2 + " wins with " + totalpoints2 + " points");
                    System.out.println("It took " + phrase2 + " " + counter2 + " rounds to win");
                } else if (totalpoints == totalpoints2) {
                    int totalrounds = counter + counter2;
                    System.out.println("It is tie game between " + phrase + " and " + phrase2);
                    System.out.println("The game lasted till " + totalrounds + " rounds");
                }
            }
        }
    }
}

【问题讨论】:

    标签: java loops while-loop


    【解决方案1】:

    你应该重新组织你的代码:

        while (true) {
            System.out.println("Enter " + phrase + " Result:");
            String team1 = keyboard.next().toLowerCase();
            if ("q".equals(team1)) {
                break;
            }
            System.out.println("Enter " + phrase2 + " Result");
            String team2 = keyboard.next().toLowerCase();
            if ("q".equals(team2)) {
                break;
            }
            if (team1.equals("w")) {
                pointsw += 2;
            } else if (team1.equals("o")) {
                pointso += 1;
            } else if (team1.equals("l")) {
                    pointsl += 0;
            }
            counter++;
            if (team2.equals("w")) {
                pointsw2 += 2;
            } else if (team2.equals("o")) {
                pointso2 += 1;
            } else if (team2.equals("l")) {
                pointsl2 += 0;
            }
            counter2++;
            totalpoints = pointsw + pointso + pointsl;
            totalpoints2 = pointsw2 + pointso2 + pointsl2;
        } // loop completed
    
        if (totalpoints > totalpoints2) {
            System.out.println(phrase + " wins with " + totalpoints + " points");
            System.out.println("It took " + phrase + " " + counter + " rounds to win");
        } else if (totalpoints < totalpoints2) {
            System.out.println(phrase2 + " wins with " + totalpoints2 + " points");
            System.out.println("It took " + phrase2 + " " + counter2 + " rounds to win");
        } else if (totalpoints == totalpoints2) {
            int totalrounds = counter + counter2;
            System.out.println("It is tie game between " + phrase + " and " + phrase2);
            System.out.println("The game lasted till " + totalrounds + " rounds");
        }
    

    【讨论】:

      【解决方案2】:

      我不完全确定,但我认为问题在于玩家 1 / 玩家 2 说“Q”之后 扫描仪仍在等待读取下一行。

              String phrase = keyboard.next();
                  System.out.println("\n"+"Enter team two:");
              String phrase2 = keyboard.next();//if player 1 types q this next() method must be resolved before it will continue to the logic
      

      所以在游戏 2 之前添加一个 if 语句询问玩家 1 是否输入了 'Q' ,如果是则计算分数并结束游戏,如果玩家 1 没有输入 'Q' 使用 else 语句继续轮到玩家 2

      p>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-25
        相关资源
        最近更新 更多