Results 1 to 7 of 7
  1. #1
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Stupid guess method....

    I have a class assignment to create a random number game and I am having issues with one of the methods....I have looked at this so much my brain feels like a kaleidoscope......any help would be so appreciated. The more I am in this class the more confused I get....

    Here are the directions for this method:
    iv. guess method
    1. Takes in an integer as a parameter representing a new guess.
    2. Compares the new guess with the answer and generates and returns a String representing an appropriate response.
    3. The response is based on:
    a. The relation of the guess and answer (too high, too low or correct).
    b. The comparison of difference between the current guess and the answer and the previous guess and the answer. (warmer, colder)
    c. Guess out of range error, if the guess is not between 0 and the max number (inclusive) (see sample run below)
    d. User has taken too many guess because numGuessesTaken is greater than maxGuessesAllowed. If this is the case set isGameOver to true.

    Here is what I have for the method:


    PHP Code:
    		public int guess(int newGuess)
    		{  
    		   
    			String response;
    			differential = Math.abs(newGuess-answer);
    		   int newDifferential = Math.abs(answer - differential);
    			Scanner keyboard = new Scanner(System.in);
    			boolean firstGuess;
    				
    			if (numGuessesTaken = 0)
    			{
    				firstGuess = true;
    			}
    			else
    			{
    				firstGuess = false;
    			}
    					
    			if (numGuessesTaken <= maxGuessesAllowed)
    			{  
    				if(answer == newGuess)
    				{
    					response = "Correct.  Would you like to play again, enter Y for yes, N for no.";
    					gameOver=true;
    					
    				}
    				else if(answer < newGuess)
    				{
    					response = "Too low";
    				}
    				else if(answer > newGuess)
    				{
    					response = "Too high";
    				}
    				else if(answer > 100 || answer < 1)
    				{ 
    					response ="Guess out of range, the guess must be between 1 and 100.";
    				}
    				     
    							 
    				if(firstGuess = false)
    				{
    				   if (newDifferential< differential)
    				   {
    					   response = "Warmer";
    					}
    				   else
    				   {
    					   response = "Colder";
    				   }
    				}
    			}
    			else
    			{
    				gameOver=true;
    				response = "Would you like to play again, enter Y for yes, N for no.";
    		   }
    		}
    Here are the errors I keep getting:
    GuessingGame.java:99: error: incompatible types (actually line 10 since I did not put my entire code up there)
    if (numGuessesTaken = 0)
    ^
    required: boolean
    found: int

  2. #2
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    55
    Rep Power
    0

    Default Re: Stupid guess method....

    = is an assignment operator not a comparison. Try ==

  3. #3
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Stupid guess method....

    That was totally it! Thanks.

    Now I have a whole new error
    GuessingGame.java:146: error: missing return statement (actually line 58 since I did not put my entire code up there)
    }
    ^
    1 error

    I have checked and double checked and all my opening brackets have corresponding closing brackets.....what am I missing here? I will admit the whole "nesting" thing has me a bit confused (like when to do it and all) could that be my issue?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default Re: Stupid guess method....

    Your method promises to return an int but nowhere in the code I can find a return <an int>; statement. The compiler also noticed that fact and started to complain ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Stupid guess method....

    Can you dumb that down for me? newGuess will be user input when I write the tester program.

    Also, I am struggling with this part:

    2. Compares the new guess with the answer and generates and returns a String representing an appropriate response.

    If at the end of the method I put

    return response;

    I get the incompatible types error...required int found string.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Stupid guess method....

    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: Stupid guess method....

    Thanks for all the help....got this part figured out....now to work on the tester.

Similar Threads

  1. Stupid Array are Stupid.
    By Army in forum New To Java
    Replies: 10
    Last Post: 04-13-2012, 06:51 PM
  2. guess game GUI
    By tomandhisjones in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 06:30 AM
  3. Easy Thing I Guess ...
    By loopsnhoops in forum New To Java
    Replies: 1
    Last Post: 02-24-2011, 10:37 PM
  4. Guess Character
    By pinkdreammsss in forum Java Applets
    Replies: 14
    Last Post: 05-05-2010, 07:28 PM
  5. guess number game
    By mistah in forum New To Java
    Replies: 10
    Last Post: 11-23-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •