0

what is wrong with part of this code? (if else, Java)

Please help me; what is wrong with part of this code. I'm just starting to learn Java, don't judge too harshly :( The program should ask for numbers until the requested number (73) is entered. My program asks only 2 times and stops. What is a mistake? Thank you in advance for your cooperation! part of this code: if (guess < guessable) { System.out.println("Guess bigger: "); } else if (guess > guessable) { System.out.println("Guess smaller: "); } else { done = true; System.out.println("You guessed right "); } Task: Need to guess the number. The number you need to guess is 73. The program prompts you to enter a number until the correct number (73) is entered. If the entered number is greater than 73, the program displays: “Guess smaller” and if it is greater than 73, then, accordingly, “Guess bigger” Example: 1. Play 2. Show the game 0. Termination Enter your choice (0-2): 2 The game has not been played 1. Play 2. Show the game 0. Termination Enter your choice (0-2): 1 Enter your name: Mikael Enter your address: China Give a guess: 80 Guess smaller: 70 Guess bigger: 73 You guessed correctly 1. Play 2. Show the game 0. Termination Enter your choice (0-2): 2 Mikael guessed the number 73 3 times

30th Sep 2023, 11:02 PM
Lena
4 Réponses
+ 3
Lena if this is in a code_bit here on sololearn playground you would have to enter all your choices in advance on the first input 45 52 63 74 84 16 And so on .... before you hit submit
30th Sep 2023, 11:10 PM
BroFar
BroFar - avatar
+ 2
Lena this code has a pre-set array of numbers based on the code GamerGeil Hd created. The array is myNum or 4 distinct guesses I hope this helps to give you insight into your code. https://code.sololearn.com/cJ39XDvnqlFy/?ref=app
30th Sep 2023, 11:42 PM
BroFar
BroFar - avatar
+ 1
The issue with your code is that it is not correctly handling the logic for prompting the user to guess the number until they guess it correctly. It seems like your code is intended to be part of a larger program that handles user input and game flow, but I'll focus on the specific issue within the code snippet you provided. The problem is that your code snippet does not have a loop to keep asking for guesses until the correct number (73) is guessed. You need to wrap the guessing logic in a loop. Here's how you can modify the code to achieve the desired behavior:
30th Sep 2023, 11:06 PM
D1M3
D1M3 - avatar
+ 1
import java.util.Scanner; public class NumberGuessingGame { public static void main(String[] args) { int guessable = 73; // The number to guess boolean done = false; Scanner scanner = new Scanner(System.in); while (!done) { System.out.print("Give a guess: "); int guess = scanner.nextInt(); if (guess < guessable) { System.out.println("Guess bigger: "); } else if (guess > guessable) { System.out.println("Guess smaller: "); } else { done = true; System.out.println("You guessed right "); } } System.out.println("Congratulations! You guessed the number!"); scanner.close(); } }
30th Sep 2023, 11:06 PM
D1M3
D1M3 - avatar