【问题标题】:trying to make a console app guessing game? is my logic correct?尝试制作控制台应用猜谜游戏?我的逻辑正确吗?
【发布时间】:2013-02-03 14:48:51
【问题描述】:

我的“如果”语句没有给我写答案,例如,如果我输入“5”,它会打印得太高,这意味着随机数较低,但如果我输入像“4”这样的较低数字,它也会打印低意味着随机数更高。它非常令人困惑,我只是想知道我在代码中的逻辑是否正确?

导入 java.util.Scanner; 导入 java.lang.Math;

公开课 GuessTest {

public static void main(String[] args) 
{
   System.out.println("Welcome to the gussing game, Try to guess the number am thinking of to win!");
   System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
   System.out.println();

    System.out.println("Am thinking of a number between 0 and 10. Try to guess it.");
    System.out.println();

   Scanner sc = new Scanner(System.in);
   String choice =  "y";

   while (!choice.equalsIgnoreCase("n"))
   {

     System.out.println("Enter the Number:");
       int guess = sc.nextInt();
       double rightNum = Math.random() *10;
       int randomNum = (int) rightNum;


       if (guess == randomNum)
       {
           System.out.println("Correct you've got it! The Number Was " + randomNum );
           System.out.println();
           System.out.println("Would you like to play again (y/n):");
           choice = sc.next();
       }
     else  if (guess < randomNum)
       {
           System.out.println("your too low!");
       }
     else if (guess > randomNum)
       {
            System.out.println("Your too high!");
       }   

}

}
}   

【问题讨论】:

    标签: java


    【解决方案1】:

    您正在计算每次while-loop 迭代的随机数。只需将计算代码移出循环即可计算一次:

    double rightNum = Math.random() *10;
    int randomNum = (int) rightNum;
    while (!choice.equalsIgnoreCase("n")) {
        //your logic...
    }
    

    由于要进行多轮游戏,可以在接受新游戏时重新计算随机数:

    double rightNum = Math.random() *10;
    int randomNum = (int) rightNum;
    while (!choice.equalsIgnoreCase("n")) {
        //your logic...
        if (guess == randomNum) {
            System.out.println("Correct you've got it! The Number Was " + randomNum );
            System.out.println();
            System.out.println("Would you like to play again (y/n):");
            choice = sc.nextLine();
            if (choice.equalsIgnoreCase("y")) {
                rightNum = Math.random() *10;
                randomNum = (int) rightNum;
            }
        }
        //your logic...
    }
    

    【讨论】:

    • 如果接受另一轮,您还需要重新计算该值
    • @MadProgrammer 没有注意到。感谢您的提示,答案已更新。
    • 无论如何还是得到了 +1 ;)
    • @MadProgrammer 我刚刚意识到我的boolean 选项是错误的(就像模因一样)
    • @MadProgrammer 如果我将选择继续放在guess == random if 子句中,当他们按 y 再次播放时它不会重新计算不同的随机数??
    【解决方案2】:

    嗯,你的逻辑是合理的......但是代码有相当缺陷。您希望程序生成一个随机数,然后让玩家根据它们是过高还是过低来猜测它,但是您让计算机每次选择一个不同的随机数。

    尝试放置

    double rightNum = Math.random() *10;
    int randomNum = (int) rightNum;
    

    就在while循环之外。这可能会解决您的问题(但当然它会产生更多关于继续选择的信息)。

    【讨论】:

    • 所以如果我将选择继续放在guess == random if 子句中,当他们按y 再次播放时它不会重新计算不同的随机数??
    • 如果你把它放在guess ==random 子句中,它仍然会创建一个新值。唯一的问题是您现在创建随机数的地方不起作用。 Luiggi Mendoza 的建议应该很有效。
    【解决方案3】:

    不要从已经建议的内容中删除任何内容,这是可以完成的简单“另一种”方式......

    public class GuessWhat {
    
        public static void main(String[] args) {
            System.out.println("Welcome to the gussing game, Try to guess the number am thinking of to win!");
            System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            System.out.println();
    
            System.out.println("Am thinking of a number between 0 and 10. Try to guess it.");
            System.out.println();
    
            Scanner sc = new Scanner(System.in);
            String choice = "y";
    
            int randomNum = -1;
            while (!choice.equalsIgnoreCase("n")) {
    
                if (randomNum < 0) {
                    randomNum = (int) (Math.random() * 10);
                }
    
                System.out.println("Enter the Number:");
                int guess = sc.nextInt();
    
    
                if (guess == randomNum) {
                    System.out.println("Correct you've got it! The Number Was " + randomNum);
                    System.out.println();
                    System.out.println("Would you like to play again (y/n):");
                    choice = sc.next();
                    randomNum = -1;
                } else if (guess < randomNum) {
                    System.out.println("your too low!");
                } else if (guess > randomNum) {
                    System.out.println("Your too high!");
                }
    
            }
    
        }
    }
    

    基本上,如果randomNum 低于0,我只需重新计算随机值。当用户猜对时,我只需将randomNum 重置为-1

    【讨论】:

    • 我试过了,没有任何区别,随机数不会被重新计算,它会保持不变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多