【问题标题】:Java Guessing number gameJava猜数字游戏
【发布时间】:2019-01-03 22:09:01
【问题描述】:

我是 JAVA 菜鸟,我一直在尝试编写数字猜谜游戏的代码,计算机从 0 到 500 中选择数字 健康)状况: 如果数字太小,用户输入 0 并且计算机猜测较小的数字 如果数字太高,用户输入 1 并且计算机猜测更高的数字

并以 5 次猜测结束游戏

这是我的代码

import java.util.Scanner;

public class Guessinggame1000 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        for (int i = 1; i <= 5; i++) {
            double r = Math.random() * 500 + 1;
            int x = (int) r;
            int n = in.nextInt();
            double high = x;
            double low = x ;
            if (n == 0) high = (int) (Math.random()) * 500 + x;
            System.out.println(((int) high));
            if (n == 1) low = (int) (Math.random()) * x;
            System.out.println(((int) low));
            if (i == 5) System.out.println("We've lost");

        }
    }
}

似乎当我运行解决方案时,我无法让计算机打印更高或更低的数字,而只能打印随机数。

任何建议将不胜感激!!! :D

【问题讨论】:

  • 这是一个让您熟悉调试器使用的好机会。当您使用调试器时,您可以在代码执行时逐行逐行执行并观察,一次一行,变量的确切值以及每个单独的操作如何修改这些值。执行此操作时,您首先在代码的哪一行观察到意外结果?那行代码的输入是什么?结果如何?你期待什么结果?为什么?
  • 您应该改用ThreadLocalRandom.current().nextInt(min, max + 1)。或者,如果您使用的是 Java Random.nextInt.

标签: java


【解决方案1】:

在这种情况下,使用doubles 听起来是个坏主意。请改用ints,以及具有有用方法的Random 对象:

    Random random = new Random();
    Scanner in = new Scanner(System.in);

    int r = random.nextInt(500)+1;
    for (int i = 1; i <= 5; i++) {
        System.out.println(r);
        int n = in.nextInt();
        if (n == 0) {
            r = random.nextInt(500-r)+r+1;
        } else if (n == 1) {
            r = random.nextInt(r-1)+1;
        }
    }

【讨论】:

  • 为了避免错误使用Random,我建议改用ThreadLocalRandom.current()
  • @Neijwiert 我不同意。
【解决方案2】:

我试图让它更简洁、更易读:

public static void main(String[] args) {
    // cleaner and easier way to produce random Int Numbers
        //because there will be no need to cast numbers anymore
        Random ran = new Random();

        Scanner in = new Scanner(System.in);

        int range = ran.nextInt(501) ;

        for (int i = 1; i <= 5; i++) {
            int n = in.nextInt();

            if (n == 0)
                range = ran.nextInt(501 - range);
            System.out.println(range);
            if (n == 1)
                range = ran.nextInt(range);
            System.out.println(range);
            if (i == 5)
                System.out.println("We've lost");

        }
    }

【讨论】:

    【解决方案3】:

    问题出在(int) (Math.random()) * 500 + x,因为您将Math.random() 转换为整数,它始终为0。尝试(int) (Math.random() * 500) + x(注意大括号更改)

    编辑

    我也知道这不是你问题的完整答案,但既然你提到你现在已经熟悉 Java,我不想提供太多帮助(这会扼杀我认为的快乐)

    【讨论】:

    • 感谢您指出这一点,我知道这个问题有更好的解决方案,但他是初学者,我不想给他完整的答案。我希望你能理解:)
    【解决方案4】:

    这是一个 MCVE,因此您可以了解如何解决此问题。不要只是复制它,而是阅读它,理解它,然后玩弄它来进行实验。如果您对任何事情感到困惑,请随时提问。

    public static void main(String[] args) {
        final int GUESSES = 5;
        final int RANGE = 100;
        final char GUESS_LOWER_KEY = '1';
        final char GUESS_HIGHER_KEY = '2';
    
        Scanner in = new Scanner(System.in);
        Random rand = new Random();
    
        // max possible guess and min possible guess
        // they will be adjusted when you say 'higher' or 'lower'
        int max = RANGE;
        int min = 0;
        for (int i = 0; i < GUESSES; i++) {
            // get a random number between the min and max
            // rand.nextInt(n) gets a number between 0 and n - 1
            // Examples:
            // nextInt(50 - 0 + 1) + 0 -> nextInt(51) + 0 -> rand from 0 to 50
            // nextInt(33 - 24 + 1) + 24 -> nextInt(10) + 24 -> rand from 24 to 33
            int guess = rand.nextInt(max - min + 1) + min;
            System.out.println("I guess: " + guess);
    
            char n = in.next().charAt(0);
            if (n == GUESS_LOWER_KEY)
                max = guess - 1; // guess - 1 to keep max inclusive
            if (n == GUESS_HIGHER_KEY)
                min = guess + 1; // guess + 1 to keep min inclusive
    
            System.out.println("min = " + min + "; max = " + max);
            if (max < min) {
                System.out.println("You lie, the answer has to be " + guess);
                return;
            }
        }
        System.out.println("I've lost!");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 2016-06-21
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多