Question: Hi there, I have a Java assigment and I am not sure if my code is correct. Please find the question attached and my asnwer
Hi there,
I have a Java assigment and I am not sure if my code is correct. Please find the question attached and my asnwer below:
THANK YOU!
package com.lab; public class Main { public static void main(String[] args) { Random random = new Random(); Scanner scanner = new Scanner(System.in); int number = random.nextInt(100); int guess = -1; while (guess!=number) { System.out.print("Try your luck now! Enter your guess: "); guess = scanner.nextInt(); if (guessout.println("WRONG! The number is too low, please try again"); } else if (guess>number) { System.out.println("WRONG!The number is too high, please try again"); } else { System.out.println("CORRECT, the number was " + number); } } } }
package com.labnsu; public class Main { public static void main(String[] args) { Random random = new Random(); Scanner scanner = new Scanner(System.in); int number = random.nextInt(100); int guess = -1; while (guess!=number) { System.out.print("Try your luck now! Enter your guess: "); guess = scanner.nextInt(); if (guessout.println("WRONG! The number is too low, please try again"); } else if (guess>number) { System.out.println("WRONG!The number is too high, please try again"); } else { System.out.println("CORRECT, the number was " + number); } } } } PP 5.4 Write a program that plays the Hi-Lo guessing game witlh numbers. The program should pick a random number between 1 and 100 (inclusive), then repeatedly prompt the user to guess the number. On each guess, report to the user that he or she is correct or that the guess is high or low. Continue accepting guesses until the user guesses correctly or chooses to quit. Use a sentinel value to determine whether the user wants to quit. Count the number of guesses and report that value when the user guesses correctly. At the end of each game (by quitting or a correct guess), prompt to determine whether the user wants to play again. Continue playing games until the user chooses to stop
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
