繁体   English   中英

数字猜谜游戏Java

[英]number guessing game java

我正在编写一个Java代码,要求用户猜测计算机的随机数,这很好,但是,我想做的是显示一条消息,该消息对应于用户进行猜测的次数。

例如,如果用户猜测1次尝试随机数,则输出将为“ Excellent!”。 如果用户猜测2-4中的答案,则尝试“ Good Job”,依此类推。

我想我没有尝试过任何东西,因为我不确定将代码粘贴在哪里?
我知道,如果猜测== 1,那么必须这样做;如果代码> 1 <= 3,则需要这样做,依此类推..等等,但是我的代码在哪里? 它是否也应该与嵌套if一起在while循环中?

这是我更新的代码,它可以运行和编译创建代码的确切方式。 感谢您的所有帮助!

public static void main(String[] args) {
        Random rand = new Random();
        int compNum = rand.nextInt(100);
        int count = 0;
        Scanner keyboard = new Scanner(System.in);
        int userGuess;  
        boolean win = false;        
        while (win == false ){
            System.out.print("Enter a guess between 1 and 100: ");
            userGuess = keyboard.nextInt();
            count++;
            if(userGuess < 1 || userGuess > 100){
                System.out.println("Your guess is out of range.  Pick a number between 1 and 100.");
                System.out.println();
            }
            else if (userGuess == compNum){
                win = true;
                System.out.println("Congratulations!  Your answer was correct! ");
            }       
            else if (userGuess < compNum){
                System.out.println("Your guess was too low.  Try again. ");
                System.out.println();
            }
            else if (userGuess > compNum){
                System.out.println("Your guess was too high.  Try again. ");
                System.out.println();
            }

        }
        if(count == 1){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("That was lucky!");
        }
        else if (count > 1 && count <= 4){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("That was amazing!");
        }
        else if (count > 4 && count <= 6){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("That was good.");
        }
        else if (count > 6 && count <= 7){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("That was OK.");
        }               
        else if (count > 7 && count <= 9){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("That was not very good.");
        }
        else if (count > 10){
            System.out.println();
            System.out.println("I had chosen " + compNum + " as the target number.");
            System.out.println("You guessed it in " + count + " tries.");
            System.out.println("This just isn't your game.");

        }
    }
        }

您可以计算迭代次数。 就像是。

boolean notCorrect = true;
int guesses = 0;
while(notCorrect){
    //Code for checking user input. 
    //break out if true
    guesses++;
}

对不起,倒退了。

别处

if(guesses < 2) {
    // display message
}
// include other if's to determine which message to display.

您可以将if语句(它决定要显示的消息)放入if语句中,以检查猜测是否正确。 但是把它从循环中拉出来。 这样,只有在用户确实正确猜出的情况下,您才运行该代码。

if (userGuess == compNum){
    win = true;
    System.out.println("Congratulations! Your answer was correct! ");
    // put message decision here...
}

记下一个int i; 并且每次用户猜错了 (不是如果用户正确地回答了答案,请执行i++ 。然后,执行类似以下操作的步骤,以便您回答正确的答案:

if(i == 0) System.out.println("Perfect!");    //Got it the first time
else if(i == 1) System.out.println("Nice!");  //Got it on the second try
else if(i <= 4) System.out.println("Good Job!");   //Got it between the second and fifth time
else if(i <= 8) System.out.println("Okay!");   //Got it between 6th and 9th time
else System.out.println("Hmmm... Try to do better next time!");  //took more than 10 times to get it right

暂无
暂无

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

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