lundi 6 février 2023

Using Random package

Okay, I want to create a simple game. I input a number, which was generated by PC using Random package and if I guess it, game over. But! I've no idea what is wrong with it.

import java.util.Scanner;
import java.util.Random; 
public class Main {
    static Scanner read = new Scanner(System.in);
    public static void main(String[] args) {
        int randomInt = new Random().nextInt(1000);
        int userInput = -1;
        System.out.println("I guessed a number\nYour turn: ");

        while (randomInt != userInput) {
            userInput = read.nextInt();
            if (randomInt > userInput) {
                System.out.println("Less than it");
            } else if (randomInt < userInput){
                System.out.println("More than that");
            }
        }
            System.out.println("That's right!");
        }
    }

I used Debug and program worked. I mean, Random did his job, generated a number, but then it didn't show me "That's right!" output when I guessed a number. It just goes like "More that that" and "More that that"...




Aucun commentaire:

Enregistrer un commentaire