簡體   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