Hi, I am stuck in Guess the number java program. I have a class template I have to use with all the methods without adding any method. I completed the program it runs but I can't get it to stop if the user is winning. it keeps running. I don't have any idea how to stop the while loop or what's the point of the isOver and isWin methods.
This is the GuessGame class
public class GuessingGame { private final int correctNumber; private int guessRemaining; private boolean won; private boolean over; public GuessingGame(int correctNumber) { this.correctNumber = correctNumber; this.guessRemaining = 11; this.won = false; this.over = false; } public int getGuessRemaining(int correctNumber) { guessRemaining --; System.out.print(guessRemaining +" Remaining guest left \n"); return guessRemaining; } public boolean isOver() { return false; } public boolean isWon() { return false; } public void guess(int guessNumber) { if(guessNumber != correctNumber && guessRemaining == 1 ) { over = true; this.printCorrectNumber(); }else if(guessNumber == correctNumber) { won = true; this.printCorrectNumber(); } else if(guessNumber > correctNumber) { System.out.print("\n The correct number is smaller than your guess \n"); } else { System.out.print("\n The correct number is larger than your guess \n"); } } public void printCorrectNumber() { if(over == true) { System.out.print("\n The gamed ended, You lost. The correct number is " + correctNumber + "\n"); } else if(won == true) { System.out.print("\n You WIN!! The number is 37..."); } } }
And this is the main
import java.util.*; public class guessMain { public static void main(String[] args) { // TODO Auto-generated method stub int guessNumber; GuessingGame guess = new GuessingGame(37); Scanner sc = new Scanner(System.in); System.out.print("*** Guess the Number *** \n"); System.out.print("Welcome , Guess the Number between 1 -100. You have 10 tries.\n"); int guessRemaining = 11; while(guess.getGuessRemaining(guessRemaining) != 0) { if(guessRemaining == 1) { guess.printCorrectNumber(); break; } System.out.print("\n Enter your guess : "); guessNumber = sc.nextInt(); guess.guess(guessNumber); // just how to stop the while loop when you have the right answer } sc.close(); } }