猜数游戏java

时间:2015-09-28 01:59:40

标签: java random

我正在编写一个java代码,要求用户猜测计算机的随机数,我很好,但是,我想要做的是有一条消息对应于用户猜测的数量。

例如,如果用户在1中猜到了随机数,那么输出中会出现“优秀!”如果用户猜到2-4的答案尝试“好工作”等等..

我想我没有尝试过任何东西,因为我不确定在哪里粘贴代码? 我知道如果猜= = 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.");

        }
    }
        }

2 个答案:

答案 0 :(得分:2)

您可以计算迭代次数。像。的东西。

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

答案 1 :(得分:0)

保持计数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