2

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"...

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • that's probably because you never guessed right. just for testing, add a print that shows the number you're supposed to guess – Stultuske Feb 06 '23 at 10:22
  • 3
    First of all: indenting matters. Why do you indent the third print, it looks like you think it should be part of the loop (but it isnt). And yeah, the code looks correct, the logical conclusion is that *you* got it wrong. And you know, for debugging ... there is no need to use a RANDOM number. Just hardcode it to a fixed value. You have to separate concerns. You want to write code that keeps looping until you give the "right" input. There is no point in testing THAT part of the code with a random number. First get THAT code right, then provide RANDOM input to it. – GhostCat Feb 06 '23 at 10:29

4 Answers4

2

I ran it myself and it runs as expected. Maybe the confusion comes from the fact that you've switched up the places where you print "Less than it" and "More than that". It should be like

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

You got something wrong with the if statements. Also the scanner should be closed with read.close() to prevent memory leaks. Since there is no reason to put the scanner in the main class I also moved that to the main function. This should work for you know.

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

        while (randomInt != userInput) {
            System.out.println(randomInt);
            userInput = read.nextInt();
            if (randomInt > userInput) {
                System.out.println("More than it");
            }
            if (randomInt < userInput){
                System.out.println("Less than that");
            }
        }
        System.out.println("That's right!");
        read.close();
    }
}
SLNM
  • 69
  • 1
  • 10
  • I also added a Sys.log to show the right number, so you can test it properly with this new code. – SLNM Feb 06 '23 at 10:29
  • A scanner with `System.in`, `System.out` or `System.err` should *not* be closed manually. These are exceptions to the rule. These are opened by the JVM, so the JVM must close them (which it does). – MC Emperor Feb 06 '23 at 12:36
  • You should properly release resources when the scanner is no longer needed, especially in the case where the scanner is used in a try-with-resources statement. In this case, if the scanner is not closed explicitly, it may not be closed automatically even if an exception is thrown. This can result in resource leaks, which can have negative impacts on performance and stability. Manually closing the scanner in this case helps ensure that resources are properly released and eliminates the risk of resource leaks. – SLNM Feb 06 '23 at 12:45
  • 1
    As I said, these are **exceptions** to the rule. The JVM [closes it](https://stackoverflow.com/a/55265002/507738). If you close it, you won't be able to open it again during the lifetime of the application. – MC Emperor Feb 06 '23 at 13:06
  • Yup, you're right! Just wanted to mention that. – SLNM Feb 06 '23 at 13:08
  • 1
    I just encountered a [StackOverflow post](https://stackoverflow.com/q/61669837/507738) where this problem arises. – MC Emperor Feb 09 '23 at 14:44
  • So should I delete this post in your opinion? I think since we discussed about the possibilities in the comments, its fine to leave it open. – SLNM Feb 09 '23 at 14:46
0

The console message "More than that" prints if randomInt is less than user input (randomInt < userInput). Wouldn't you want it to print that if it is more than user input? The inverse condition applies to the other message. Could this be the issue? Otherwise, as already suggested, use a print statement to print the number out so you can test the correct guess.

0

I tried your code, and basically it works. The only problem I see is that you print the wrong string.

import java.util.Scanner;
import java.util.Random;

public class Test {

    static Scanner read = new Scanner(System.in);

    public static void main(String[] args) {
        int randomInt = new Random().nextInt(10);
        int userInput = -1;
        System.out.println("I guessed a number\nYour turn: ");

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

Some tips:

  1. Do not use such big numbers for tests
  2. Instead of using a while loop use a do while loop
Aledlp5
  • 53
  • 7