【问题标题】:number guessing game java数字猜谜游戏java
【发布时间】:2015-09-28 01:59:40
【问题描述】:

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

例如,如果用户在 1 次尝试中猜到了随机数,则会输出“Excellent!”如果用户在 2-4 中猜到了答案,则尝试“Good Job”等等..

我想我什么都没试过,因为我不确定在哪里粘贴代码?
我知道如果guess == 1 则必须这样做,否则如果代码> 1

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

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.");

        }
    }
        }

【问题讨论】:

  • 您需要做的是向我们展示有问题的代码并解释您的尝试。您的问题有被关闭的危险,因为除了您的目标之外,您没有提供任何相关细节。首先决定如何将猜测次数映射到“奖励”短语,然后编写代码来实现该映射。
  • 创建一个函数,它将尝试的次数作为参数,并返回一个包含您想要的消息的字符串。您需要使用某种控制结构来执行此操作,最简单的可能只是一堆 if-else 语句。至于这个问题,这里是题外话,因为实际上很难给你一个简洁的答案,包括一些你试图解决问题的代码会很好。
  • 对不起,就像我在这里说的很新,看到一些关于代码的帖子,但它与我的问题无关。我想我需要帮助的是如何将其放入我的代码中,是否应该全部放在一个大的嵌套循环中?很抱歉,我正在尝试找出如何添加我的代码以显示我所拥有的..

标签: java random


【解决方案1】:

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

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

【讨论】:

  • 谢谢@Mikejg101。我知道我必须在这里做这样的事情,但我不确定将代码放在哪里......它仍然在我的 while 循环中还是与我的其他嵌套 if 嵌套?
  • 没问题。我很乐意提供帮助。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2022-01-06
    相关资源
    最近更新 更多