最后一个问题:Mastermind Java程序

时间:2016-12-27 03:28:11

标签: java

我对我的代码有两个问题。

  1. 为什么输出"哎呀请输入1到6之间的数字"当我输入一个介于1和6之间的数字时。当我尝试更具体并制作一个else if语句时,当我输入一个不在1到6之间的数字时没有任何反应。

  2. 如何重新启动程序?在我的代码中,有一个if语句 当用户输入"再次播放时#34;我的注释掉了 Mastermind.main()重新启动该计划,但这不起作用。

  3. 以下是代码:

    import java.util.Scanner;
    
    public class Mastermind {
    
        public static void main (String [] args) {
            // boolean variable to signal when the game is over.
            boolean done = false;
    
            // Scanner object
            Scanner scanner = new Scanner(System.in);
    
            // sets the value to twelve outside the loop so it doesn't set back each time.
            int guesses = 12;
    
           System.out.println("Please enter a number between 1-6 to begin (or \"quit\") to exit.");
    
            // while loop for the game
            while (!done) {
    
                //System.out.println("Please enter a number between 1-6  (or \"quit\") to exit the game:");
    
                // user input
                String input = scanner.nextLine();
                int number = 0; //Just initialized to some number
    
    
                // checks to see if the user wants to quit the game.
                if (input.equalsIgnoreCase("quit")) {
                    System.out.println("Goodbye!");
                    done = true;
                    scanner.close();
                }
    
                else{
    
                    try{
                        //Trying to see if the input was a number
                        number = Integer.parseInt(input);
    
    
                    }
                    catch(Exception e){
                        //The input wasn't an integer, it's invalid the starts loop again.
                        System.out.println("Invalid input.");
                        continue;
                    }
                }
    
                // defines necessary int variables
                int random1 = (int) (Math.random() * 7);
                int random2 = (int) (Math.random() * 7);
                int random3 = (int) (Math.random() * 7);
                int random4 = (int) (Math.random() * 7);
    
               // If the user doesn't and decides to play, it runs this code.
                // checks to see if the user enters a number between 1-6
                if (number >= 1 && number <= 6) {
    
                    if (number == random1) {
                        System.out.println("You guessed a correct number!");
                       guesses--;
                       System.out.println("guesses = " + guesses);
    
                    }
                    else if (number == random2) {
                        System.out.println("You guessed a correct number!");
                        guesses--;
                        System.out.println("guesses = " + guesses);
    
    
                    }
                    else if (number == random3) {
                        System.out.println("You guessed a correct number!");
                        guesses--;
                        System.out.println("guesses = " + guesses);
                    }
                    else if (number == random4) {
                        System.out.println("You guessed a correct number!");
                        guesses--;
                        System.out.println("guesses = " + guesses);
    
                    }
                    else {
    
                        System.out.println("Sorry that's not one of the numbers! Try again.");
                        guesses--;
                        System.out.println("guesses = " + guesses);
                    }
                }
    
    
                if (guesses == 0){
    
                    System.out.println("You've run out of guesses. To play again, enter \"play again\". Otherwise, enter or \"quit\")");
                    if (input.equalsIgnoreCase("play again")){
                        // how do I restart the program?
                        //Mastermind.main(); // QUESTION 2
                    }
                    else if (input.equalsIgnoreCase("quit")){
                        System.out.println("Goodbye!");
                        done = true;
                        scanner.close();
                    }
                    else {
                        System.out.println("Goodbye!");
                        done = true;
                        scanner.close();
                    }
                }
                else { //QUESTION 1
    
                    System.out.println("Oops! Please choose a number between 1 and 6");
                }
            }
        }
    }
    

1 个答案:

答案 0 :(得分:1)

每当guesses == 0评估为false时,您每次都会在循环中打印该消息。您可能只需要切换两个块的顺序。而不是:

if (number >= 1 && number <= 6) {
    ...
}
if (guesses == 0) {
    ...
}
else { //QUESTION 1
    System.out.println("Oops! Please choose a number between 1 and 6");
}

使用此:

if (number >= 1 && number <= 6) {
    ...
}
else { //QUESTION 1
    System.out.println("Oops! Please choose a number between 1 and 6");
}
if (guesses == 0) {
    ...
}

关于重新启动程序:如果我正确读取逻辑,您只需将done设置为false并将guesses重置为12。

另外两个逻辑点。首先,在检测到用户已输入continue后,您应该break"quit"。其次,似乎你为每个用户猜测产生了四个新的随机整数。我不知道这是不是你想要的,但你可能想稍微改变逻辑。这也可能会影响重启逻辑。