Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Guess the number using class

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Guess the number using class

    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();
     
    	}
     
     
    }
    Last edited by Midlsk; October 25th, 2019 at 06:16 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Guess the number using class

    how to stop the while loop
    The break statement will exit a loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Guess the number using class

    Quote Originally Posted by Norm View Post
    The break statement will exit a loop.
    I know the break statement will break the loop. I am struggling with the condition for the if statement.
    If I put if (guess.isOver() == false) then the break statement, it will break the loop in the first time and not when the user wins. if I put True it will not break when the user wins because there is nothing to change the method to True.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Guess the number using class

    break the loop ... when the user wins
    Can you detect when the user wins?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2019
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Guess the number using class

    So the user wins when he enters the number 37. I can't use the variable in the main class to compare it since it's private and I can't change that. So I only can use a method.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Guess the number using class

    Perhaps the logic needs to be changed so that a method returns a specific value when the user has won. For example have guess() return true when the game is over.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2019
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Guess the number using class

    Quote Originally Posted by Norm View Post
    Perhaps the logic needs to be changed so that a method returns a specific value when the user has won. For example have guess() return true when the game is over.
    I can't change the logic for methods or constructor. The teacher want it this way. He gave us the methods template. We are not allowed to add or change any method. I was thinking I am not using isOver and isWon right. Should I do the while loop in the class instead of main. Or find another condtion for the loop.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Guess the number using class

    I can't change the logic for methods or constructor.
    That's strange. The logical choice would be for guess to say there was a win.
    After the call to the guess method, how can the program determine if the game was won?

    Can you change isOver or isWon? Both those methods are useless since they always return false.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Midlsk (October 27th, 2019)

Similar Threads

  1. Word Guess
    By Jessi09 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 6th, 2014, 12:26 PM
  2. guess program problems.
    By shin777 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2013, 07:15 AM
  3. Help with 3 guess Dice game
    By tabmanmatt in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2012, 07:42 PM
  4. Guess Number (Frame & btnHandler)!!!! HELP
    By xplox2011 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2011, 04:08 AM
  5. Prime Number Program for class
    By chachunga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2011, 12:05 AM