试图让控制台应用猜测游戏?我的逻辑是否正确?

时间:2013-02-19 02:17:51

标签: java

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

import java.util.Scanner;  import 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!");
       }   

}

}
}   

3 个答案:

答案 0 :(得分:4)

您正在计算每个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...
}

答案 1 :(得分:0)

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

尝试放置

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

就在while循环之外。这可能会解决你的问题(当然,它会在继续选择方面创造更多)。

答案 2 :(得分:0)

不要偏离已经建议的内容,这是一种简单的“另一种”方式......

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