Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; public class GuessNumberApp { public static void main ( String [ ] args ) { Game game = new Game ( ) ;

import java.util.Scanner;
public class GuessNumberApp {
public static void main(String[] args){
Game game = new Game();
game.displayWelcomeMessage();
Scanner scanner = new Scanner(System.in);
// New game loop
while (true){
game.generateNumberToBeGuessed();
game.displayPleaseGuessMessage();
// Inner game loop
while (true){
int userGuess = scanner.nextInt();
game.makeGuess(userGuess);
if (game.isCorrectGuess()){
game.displayCorrectGuessMessage();
break; // Exit inner loop if guess is correct
} else {
displayGuessFeedback(game.getNumber(), userGuess);
game.displayGuessAgainMessage();
}
}
// Ask if the user wants to play again
System.out.println("Do you want to play again? (Type 'y' for yes, 'n' for no)");
String playAgain = scanner.next().toLowerCase();
if (playAgain.equals("n")){
break; // Exit outer loop if user types 'n'
}
}
scanner.close();
}
// Method to display feedback based on the difference between the guess and the number to be guessed
private static void displayGuessFeedback(int numberToGuess, int userGuess){
int difference = Math.abs(numberToGuess - userGuess);
if (difference >10){
System.out.println("Way too "+(userGuess > numberToGuess ? "high" : "low")+"! Guess again.");
} else {
System.out.println("Too "+(userGuess > numberToGuess ? "high" : "low")+"! Guess again.");
}
}
}
in java

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions