简体   繁体   中英

Java Guessing number game

I'm rookie with JAVA and I've been trying to writing code for number guessing game which the computer pick the number from 0 - 500 condition: If the number is too low the user enter 0 and the computer guess lower number If the number is too high the user enter 1 and the computer guess higher number

And ends game with 5 guess

here's my code

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

        }
    }
}

It seems like when I ran the solution , I cannot get the computer to print higher or lower number but just the random numbers.

Any suggestion would be much appreciated !!! :D

In this case, it sounds like a bad idea to use double s. Use int s instead, and a Random object that has useful methods:

    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;
        }
    }

i tried to make it cleaner and more readable for you:

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

        }
    }

The problem is at (int) (Math.random()) * 500 + x since you are casting Math.random() to an integer it is always 0. Try (int) (Math.random() * 500) + x (note the brace changes)

EDIT

Also I know this is not the full answer to your question, but since you mentioned that you are getting familiar with Java now, i don't want to help too much (that kills the joy i think)

Here's an MCVE so you can see how to solve this problem. Don't just copy it but instead read it, understand it, and play around with it to experiment. If you're confused about anything then feel free to ask.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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