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

2
  • that's probably because you never guessed right. just for testing, add a print that shows the number you're supposed to guess Commented Feb 6, 2023 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. Commented Feb 6, 2023 at 10:29

5 Answers 5

1

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"); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Well, I guess I need to check it twice before asking for help
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(); } } 

8 Comments

I also added a Sys.log to show the right number, so you can test it properly with this new code.
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).
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.
As I said, these are exceptions to the rule. The JVM closes it. If you close it, you won't be able to open it again during the lifetime of the application.
I just encountered a StackOverflow post where this problem arises.
|
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.

Comments

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

Comments

-1

Here's a way to do it:

import java.util.Random; import java.util.Scanner; public class CricketGame { // Variables for the game private static int runs = 0; private static int ballsFaced = 0; private static boolean isOut = false; private static Random random = new Random(); // Method to simulate batting private static void bat() { // Loop for a maximum of 6 balls (1 over) while (ballsFaced < 6 && !isOut) { ballsFaced++; System.out.println("Ball " + ballsFaced + ": "); int outcome = random.nextInt(7); // Random outcome between 0-6 switch (outcome) { case 0: System.out.println("You're out! Game over."); isOut = true; break; case 1: System.out.println("1 run"); runs += 1; break; case 2: System.out.println("2 runs"); runs += 2; break; case 3: System.out.println("3 runs"); runs += 3; break; case 4: System.out.println("4 runs (boundary!)"); runs += 4; break; case 5: System.out.println("5 runs"); runs += 5; break; case 6: System.out.println("6 runs (six!)"); runs += 6; break; default: break; } } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Welcome to the Cricket Game!"); System.out.println("You will face 6 balls. Try to score as many runs as possible."); // Start the batting phase bat(); if (isOut) { System.out.println("You were out after " + ballsFaced + " balls."); } else { System.out.println("You faced all 6 balls!"); } System.out.println("Your total score: " + runs + " runs."); System.out.println("Game Over!"); scanner.close(); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.