Java Guessing数字游戏

时间:2019-01-03 13:17:21

标签: java

我是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

4 个答案:

答案 0 :(得分:1)

在这种情况下,使用double听起来是个坏主意。请改用int和一个具有有用方法的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;
        }
    }

答案 1 :(得分:1)

我试图让它更干净,更易读:

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

        }
    }

答案 2 :(得分:0)

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

编辑

我也知道这不是您问题的完整答案,但是由于您提到您现在已经熟悉Java,所以我不想提供太多帮助(这消除了我的喜悦)

答案 3 :(得分:0)

这里是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!");
}