2014-09-19 63 views
-1

我想创建一个数字猜测程序,用户输入一个数字,程序会告诉您输入是否太高,太低或正确。程序选择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{ 
    } 

回答

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子句永远不会被调用。你有条件超出范围,大于随机,小于随机,等于随机。您可以安全地删除该死的代码。

+0

所以“return”在if语句中结束程序? – Nocturne 2014-09-19 04:33:08

+0

返回标记方法的结束。在这种情况下,你在程序的主要方法main()中,所以它会结束程序。 – nostromo 2014-09-19 04:33:46

+0

@nostromo你好吗?只是友好的点你有没有看到这条线?如果(guess2> = 100 || guess2 <= 0){它在0和100之间吗? – 2014-09-19 04:36:01

0

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

+1

添加一些解释到您的文章,这可能有助于他人迅速理解它。 – 2014-09-19 04:46:53

1

有2个主要问题与您的代码:

首先

您未能投随机变量马上所以随机值的值始终为0。

合适的款式是:

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

你如果结构可怕:

例如:

if(guess >= 100 || guess <= 0){ 
  1. 你说猜测是且等于100
  2. 你说猜测是少且等于0
  3. 您使用||操作数,而不是&&检查数量在0范围100

试试这个:

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

说明:如果居兴数量为100和0之间,做以下。如果它不告诉用户他或她不在该范围内。

+0

所以“if”语句应该让它在开始时再次定义,然后在声明结尾处出现错误?我有点困惑,为什么 if(guess <= 100 && guess> = 0)是需要的,因为程序员已经知道这一点。 – Nocturne 2014-09-19 04:25:33

+0

雅我改变了我的代码,你知道Java中的循环概念吗? – 2014-09-19 04:27:09

+0

好吧,我学到了一段时间和for循环,但老师从来没有真正说过如何组织格式的任何事情。 – Nocturne 2014-09-19 04:27:52

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); 
+0

当你赢了,打破循环,否则你会重复所有的方式通过猜测... – MadProgrammer 2014-09-19 05:01:24

+0

谢谢你的建议 – 2014-09-19 06:24:24