数字猜测程序错误?

时间:2014-09-19 04:05:46

标签: java eclipse

我正在尝试创建一个数字猜测程序,用户输入一个数字,程序会告诉您输入是否太高,太低或正确。程序选择0-100之间的随机数。问题是当我开始测试程序时,当我选择1时,程序告诉我猜测太高了。但这是错误的,因为程序只能做整数。并且如果用户输入的数字不等于随机数,则1必须太低。 有人可以看看我的代码,看看是否有什么看起来很奇怪。

int guess;
    int guess2;
    int guess3;
    int guess4;
    int guess5;
    int random = (int)Math.random() * 100;
    Scanner input = new Scanner(System.in);

    System.out.println("Try to guess a number between 0-100. You have five chances.");
    guess = input.nextInt();
    if(guess >= 100 || guess <= 0){
        System.out.println("Error, that is not a number between 0-100.");
    } else if(guess > random){
        System.out.println("The guess is too high.");
    } else if(guess < random){
        System.out.println("The guess is too low.");
    } else if(guess == random){
        System.out.println("YOU WIN");
    } else{
    }

    System.out.println("Second guess.");
    guess2 = input.nextInt();
    if(guess2 >= 100 || guess2 <= 0){
        System.out.println("Error, that is not a number between 0-100.");
    } else if(guess2 > random){
        System.out.println("The guess is too high.");
    } else if(guess2 < random){
        System.out.println("The guess is too low.");
    } else if(guess2 == random){
        System.out.println("YOU WIN");
    } else{
    }

    System.out.println("Third guess.");
    guess3 = input.nextInt();
    if(guess3 >= 100 || guess3 <= 0){
        System.out.println("Error, that is not a number between 0-100.");
    } else if(guess3 > random){
        System.out.println("The guess is too high.");
    } else if(guess3 < random){
        System.out.println("The guess is too low.");
    } else if(guess3 == random){
        System.out.println("YOU WIN");
    } else{
    }

    System.out.println("Fourth guess.");
    guess4 = input.nextInt();
    if(guess4 >= 100 || guess4 <= 0){
        System.out.println("Error, that is not a number between 0-100.");
    } else if(guess4 > random){
        System.out.println("The guess is too high.");
    } else if(guess4 < random){
        System.out.println("The guess is too low.");
    } else if(guess4 == random){
        System.out.println("YOU WIN");
    } else{
    }

    System.out.println("Last guess.");
    guess5 = input.nextInt();
    if(guess5 >= 100 || guess5 <= 0){
        System.out.println("Error, that is not a number between 0-100.");
    } else if(guess5 > random){
        System.out.println("The guess is too high.");
    } else if(guess5 < random){
        System.out.println("The guess is too low.");
    } else if(guess5 == random){
        System.out.println("YOU WIN");
    } else{
    }

4 个答案:

答案 0 :(得分:2)

看起来不错,但你需要改变这一行的演员阵容:

int random = (int)Math.random() * 100;

为:

int random = (int)(Math.random() * 100);

目前您的随机数始终为0.为什么?因为您将Math.random()转换为int,而在Java中则意味着小数点后的所有内容都将丢失。 (因此,0.85将变为0. 0 * 100 = 0.)通过添加括号,我们在Math.random()乘以100之后进行投射。

另请注意,您的随机数可能为0但永远不会为100. Math.random()返回一个大于或等于0且小于1的数字。当您将其转换为int时,99.9999将变为99

如果您希望它真的是0-100,那么你会想要这个:

int random = (int)(Math.random() * 101);

此外,您可能希望游戏在玩家获胜后终止。所以改变这个:

System.out.println("YOU WIN");

到此:

System.out.println("YOU WIN");
return;  //end

最后,永远不会调用您的空else子句。您的条件超出范围,大于随机,小于随机,等于随机。您可以安全地删除该死代码。

答案 1 :(得分:1)

您的代码有两个主要问题:

首先

您未能立即投射随机变量,因此随机值的值始终为0.

正确的风格是:

int random = (int)(Math.random() * 100);

<强>第二

你的if结构很糟糕:

例如:

if(guess >= 100 || guess <= 0){
  
      
  1. 你说猜测大于等于100
  2.   
  3. 你说猜测小于等于0
  4.   
  5. 你用过||操作数而不是&&来检查数字是否在0和100
  6. 的范围内   

试试这个:

if (guess <= 100 && guess >= 0) {
        if (guess > random) {
            System.out.println("The guess is too high.");
        } else if (guess < random) {
            System.out.println("The guess is too low.");
        } else if (guess == random) {
            System.out.println("YOU WIN");
        }
     } else {
        System.out.println("Error, that is not a number between 0-100.");
    }

说明:如果gussing number介于100和0之间,请执行以下操作。如果没有告诉用户他或她不在范围内。

答案 2 :(得分:0)

int random = (int)Math.random() * 100;更改为int random = (int)(Math.random() * 100);

答案 3 :(得分:0)

你可以像这样做你的代码。很容易你可以使用for循环

    int guess;
    int random = (int) (Math.random() * 100);
    Scanner input = new Scanner(System.in);

    System.out.println("Try to guess a number between 0-100. You have five chances.");
    for (int i = 0; i < 5; i++) {
        System.out.println("Chance " + (i + 1));
        guess = input.nextInt();
        if (guess >= 100 || guess <= 0) {
            System.out.println("Error, that is not a number between 0-100.");
        } else if (guess > random) {
            System.out.println("The guess is too high.");
        } else if (guess < random) {
            System.out.println("The guess is too low.");
        } else if (guess == random) {
            System.out.println("YOU WIN");
            break;
        }
    }
    System.out.println(random);