繁体   English   中英

Java猜数字游戏

[英]Java Guessing number game

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

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

我试图让它更清晰,更易读:

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

        }
    }

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

编辑

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM