随机数猜测游戏的逻辑错误

时间:2014-04-08 23:48:16

标签: java random

在此程序中,计算机生成一个随机数(1-100之间),用户试图猜测它。 运行直到用户正确猜出数字。需要打印出正确猜出数字之前所花费的总尝试次数。 程序运行正常,但存在逻辑错误。当我正确猜出数字时,没有打印出来;程序就此停止了。

import java.util.*;
public class Problem3 {


  public static void main(String[] args) { 

    Random rand = new Random();
    Scanner console = new Scanner(System.in);

    int num = rand.nextInt(100)+1;
    System.out.println(num); //prints out the random number so I know test works correctly
    int count = 0;
    System.out.println("Guess the random number. The number is between 1 and 100. Enter it below");
    console = new Scanner(System.in);
    int guess = console.nextInt();
    while(guess != num){  //while guess is not = to number  
      if(guess < num){             //if less than num
        System.out.println("Your guess is too low"); 
        System.out.println("Guess the random number. The number is between 1 and 100. Enter it below");
        guess = console.nextInt();
        count++;
      }else if( guess > num){      //if greater than num   
          System.out.println("Your guess is too high");
          System.out.println("Guess the random number. The number is between 1 and 100. Enter it below");
          guess = console.nextInt();
          count++;
      }else{
        count++;
        System.out.println("You guessed correctly after " + count + " tries!");
    }
    }
  }

}

2 个答案:

答案 0 :(得分:2)

实际上你永远不会进入else阶段,因为当猜到数字时,while代码不会执行,因此else代码永远不会执行。所以在那之后数字被猜到了,所以条件是false,退出while循环和System.out.print恭喜消息

将它放在你的while循环之外&#34;在结尾&#34;:

 System.out.println("You guessed correctly after " + count + " tries!");

这是您的代码应该是什么样子:

while(guess != num){  //while guess is not = to number  
      if(guess < num){             //if less than num
        System.out.println("Your guess is too low"); 
        System.out.println("Guess the random number. The number is between 1 and 100. Enter it below");
        guess = console.nextInt();
        count++;
      }else{      //if greater than num   
          System.out.println("Your guess is too high");
          System.out.println("Guess the random number. The number is between 1 and 100. Enter it below");
          guess = console.nextInt();
          count++;
      }
    }

  System.out.println("You guessed correctly after " + ++count + " tries!");

答案 1 :(得分:0)

当猜到正确的数字时,你的while循环条件变为false。这使得其他无法访问。

修改

以下是我如何解决您的问题。如果您发现自己重复代码,通常意味着有改进的机会。不仅代码更少,而且更容易阅读和跟踪正在发生的事情。我并没有试图窃取答案(@AmirBawab首先发现了这个问题),但我非常相信不仅可以使代码有效,而且代码可以由其他人维护和阅读。希望你继续学习,你会记住这一点。快乐的编码!

public static void main(String[] args) {

    Random rand = new Random();
    Scanner console = new Scanner(System.in);

    int num = rand.nextInt(100) + 1;

    int count = 0;

    while (true) {
        System.out.println("Guess the random number. The number is between 1 and 100. Enter it below");
        int guess = console.nextInt();
        count++;
        if (guess < num) {
            System.out.println("Your guess is too low");
        } else if (guess > num) {
            System.out.println("Your guess is too high");
        } else {
            System.out.println("You guessed correctly after " + count + " tries!");
            break;
        }
    }

    console.close();

}