退出策略不起作用

Exit strategy doesn't work

我正在尝试在一个小游戏中制定退出策略。 但是当我输入 yes 时,它会再次运行 te 脚本,但是当我完成时,脚本不会询问:

Do you want to try again

但是没有使用退出策略又开始了。

第二次 运行 时如何使退出策略起作用?

package steenpapierschaar1;


public class SteenPapierSchaar1 {

    public static void main(String[] args) {

        final int rock = 1 ;   // waarde van Rock
        final int paper = 2;  // waarde van paper
        final int scissor = 3;// waarde van scissor
        Scanner KeyBoard = new Scanner (System.in);// naam scanner input
        Random rand = new Random();
        boolean playing = true;
        boolean ans = false;
        String ans0 = "";


        while(playing == true){
            int AI = rand.nextInt((3 - 1) +1) + 1;// maakt int AI een random getal nog niet af moet een range in komen
               System.out.println("choose 1 for rock, 2 for paper and 3 for scissor"); // string met keuze voor User
            int UserChoice = KeyBoard.nextInt();// waarde van UserChoice

                if (AI == rock && UserChoice == paper || AI == paper && UserChoice == scissor || AI == scissor && UserChoice == rock){
                System.out.println("You win");
                if (AI == rock)
                System.out.println("The computer did rock");
                if (AI == paper)
                System.out.println("The computer did Paper");
                if (AI == scissor)
                System.out.println("The computer did Scissor");
                }

                else if (AI == UserChoice) {
                System.out.println("Draw");
                if (AI == rock);
                System.out.println("The Computer did rock");
                if (AI == paper)
                System.out.println("The Computer did paper");
                if (AI == scissor)
                System.out.println("The Computer did scissors");
                }

                else if (AI == rock && UserChoice == scissor || AI == paper && UserChoice == rock || AI == scissor && UserChoice == paper){
                System.out.println("You Lose");
                if (AI == rock)
                System.out.println("The Computer did rock");
                if (AI == paper)
                System.out.println("The Computer did paper");
                if (AI == scissor)
                System.out.println("The Computer did scissors");
                }              

                 while (ans == false){
            System.out.println("Do you want to try again?");
            ans0 = KeyBoard.nextLine();
            ans0 = ans0.toLowerCase();
            switch(ans0){
                case "yes":
                    ans = true;
                    playing = true;
                    break;
                case "no":
                    ans = true;
                    playing = false;
                    break;
                }                
        }        
    }
}

将您的开关更改为:

switch(ans0){
        case "yes":
            ans = true;
            break;
        case "no":
            playing = false;
            break;

你的 switch 中的 break 只会让你脱离 switch。要退出循环,请执行以下操作:

switch(ans0){
        case "yes":
            ans = true;
            break;
        case "no":
            playing = false;
            break;

这样你的播放标志设置为false,你将退出循环

我通常会遵循的一些建议(不能在评论中添加所以..)-

1. Name your final variables in all CAPITAL.
2. Change while(playing == true) to while(playing)
3. Change if (AI == rock && UserChoice == paper || AI == paper && UserChoice == scissor || AI == scissor && UserChoice == rock) to 
          if ((AI == rock && UserChoice == paper) || (AI == paper && UserChoice == scissor) || (AI == scissor && UserChoice == rock))
4. Change while (ans == false) to while (!ans)
5. Change "Do you want to try again?" to "Do you want to try again (yes/no)?"

答案已经给出:)

因为在 while 条件之后没有 { } 块,while 块只有它后面的一行。所以它只是无休止地重复 SysOut。

这就是您正在尝试做的更多事情。

****EDIT*** 正如其他答案所述,开关语句存在第二个问题。试试这种方式退出。

        while (ans == false) {  // start of while block
          System.out.println("Do you want to try again?");
          ans0 = KeyBoard.nextLine();
          ans0 = ans0.toLowerCase();
          switch(ans0){
            case "no":
              playing = false;
            case "yes":
              ans = true;
          }
        }    // end of while block

以上将循环直到答案 "yes" 或 "no",此时它设置 ans = true 以退出 while (ans == false) 循环。

如果他们回答"no"它会看到playing = false退出上面的playing == true循环,否则它会再次循环游戏。

你已经完成了工作,所以我觉得可以发布这个。这就是我想出的。我希望这就是你要找的。我对它进行了很多重构,使其更易于阅读和 运行。至少有一个问题是没有任何输入验证。

public class Game {

    private enum Choice{
        ROCK(1),
        PAPER(2),
        SCISSORS(3);

        int value;
        Choice(int i){
            value = i;
        }

        public static Choice lookup(int i){
            for(Choice c : values()){
                if(c.value == i){
                    return c;
                }
            }
            return null;
        }
    }

    private enum Result{
        WIN,LOSE,DRAW
    }

    public static void main(String[] args) {

        Scanner keyBoard = new Scanner(System.in);// naam scanner input
        Random rand = new Random();
        boolean playing = true;

        while(playing){
            int ai = rand.nextInt((3 - 1) +1) + 1;// maakt int AI een random getal nog niet af moet een range in komen
            Choice aiChoice = Choice.lookup(ai);
            Choice userChoice = getUserChoice(keyBoard);
            Result result = calculateResult(aiChoice,userChoice);

            System.out.println(getResultMessage(result));
            System.out.println(getComputerChoiceMessage(aiChoice));

            System.out.println("Do you want to play again?");
            playing = keyBoard.next().toLowerCase().equals("yes");
        }
    }

    private static String getComputerChoiceMessage(Choice aiChoice) {
        switch (aiChoice){
            case PAPER:
                return "The computer did paper";
            case ROCK:
                return "The computer did rock";
            case SCISSORS:
                return "The computer did scissors";
        }
        return "";
    }

    private static String getResultMessage(Result result) {
        switch (result){
            case WIN:
                return "You win!";
            case LOSE:
                return "You lost!";
            case DRAW:
                return "Draw!";
        }
        return "";
    }

    private static Result calculateResult(Choice aiChoice, Choice userChoice) {
        if(aiChoice == userChoice){
            return Result.DRAW;
        }
        if(aiChoice == Choice.ROCK && userChoice == Choice.PAPER ||
                aiChoice == Choice.PAPER && userChoice == Choice.SCISSORS ||
                aiChoice == Choice.SCISSORS && userChoice == Choice.ROCK){
            return Result.WIN;
        }
        return Result.LOSE;
    }

    private static Choice getUserChoice(Scanner keyBoard) {
        System.out.println("choose 1 for rock, 2 for paper and 3 for scissor");
        return Choice.lookup(keyBoard.nextInt());
    }
}

和一些示例输出

choose 1 for rock, 2 for paper and 3 for scissor
1
Draw!
The computer did rock
Do you want to play again?
yes
choose 1 for rock, 2 for paper and 3 for scissor
3
Draw!
The computer did scissors
Do you want to play again?
no

Process finished with exit code 0