繁体   English   中英

退出策略不起作用

[英]Exit strategy doesn't work

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

您想再试一次吗

但它会在不使用退出策略的情况下再次开始。

当出口策略第二次运行时,我需要如何使其退出工作?

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;
                }                
        }        
    }
}

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

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

****编辑***,因为其他答案描述了切换语句存在第二个问题。 尝试以这种方式退出。

        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

上面的代码将循环播放,直到答案为“是”或“否”为止,此时将ans = true设置为退出while(ans == false)循环。

如果他们回答“否”,则将看到playing = false退出上述playing == true循环,否则将再次循环游戏。

使用以下方法更改开关:

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

break你的交换机只打破你出你的开关。 要退出循环,请执行以下操作:

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)?"

答案已经给出了:)

您已经完成了工作,因此发布此内容我觉得不错。 这就是我想出的。 我希望这就是您要寻找的。 我对其进行了很多重构,以便于阅读和运行。 至少一个问题是,没有任何输入验证。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM