• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Method not returning correct value

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So working on methods, and I am having some issues with the fight method, it should return the number of attempts used to correctly guess the color, but if you take 2 attempts it returns the value 4. What am I doing wrong here?

 
Master Rancher
Posts: 240
25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I'd recommend you re-format your code using your IDEs capabilities - it's really hard to read as is due to the symbols not lining up.

As for the actual problem. What is "num1"? Why is that an argument and why are you using it in a for loop?
Unfortunately there are some logic problems with the method. I recommend writing down (on paper or comments) what the logic is for this method, and then code only that logic. Can you explain the purpose of the method and your idea to implement the method as a step-by-step process? If you can do this, you are most of the way there.
 
Rancher
Posts: 3968
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

//This method returns an int value and has one parameter.


That juar says what the code says.
To be useful the comments should  describe what is in the int value it returns and what is in the one parameter
 
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:To be useful the comments should  describe what is in the int value it returns and what is in the one parameter


Right. In other words, don't be like Captain Obvious when writing comments: https://knowyourmeme.com/photos/67034-captain-obvious
 
Colby Moreland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, so the count is set for the value i and this is the number of time the for loop runs, but if it only runs twice the count = 4. I tried to adjust the initial value of i from 2 to 1 and to 0 but these just came out to 0. Idk what the loop is doing or what I entered in wrong to cause the value to return the incorrect value.
 
Marshal
Posts: 26131
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Colby Moreland wrote:Sorry, so the count is set for the value i and this is the number of time the for loop runs



No, that's not true.

but if it only runs twice the count = 4.



What makes you think the loop only runs twice? That isn't true either.

I would expect the loop to terminate when the user enters the correct answer, but you aren't doing that.
 
Paul Clapham
Marshal
Posts: 26131
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me explain what I meant about the workings of the loop, since I didn't before and that wasn't all that helpful.

You wrote this loop:



The loop counter starts at 1 and it increases by one for each time through the loop. That's the first and last parts of the loop declaration. The middle part says "When should I go through the loop again?"

So if i <= num1 then the loop runs one more time. Which means that when i > num1, the loop is finished. Now remember that you passed 3 to the method as the value of num1, so therefore the loop is finished when i > 3. That's where "4" comes from.

Also note that the loop is executed when i is 1, 2, and 3. So it runs three times, not twice.
 
Colby Moreland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhh, so I should swap the if statement and the for loop and also change the middle condition to just and equals?
 
Colby Moreland
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually maybe swap from the if statement to a while loop that runs while the word doesn’t equal blue.
 
Paul Clapham
Marshal
Posts: 26131
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Colby Moreland wrote:Actually maybe swap from the if statement to a while loop that runs while the word doesn’t equal blue.



Yeah, that's a much better idea. Although I expect you might also want something to limit the number of times the user can guess, to prevent the user having to guess forever without success. Not sure how that would fit into the design though so maybe leave that for later.
 
Colby Moreland
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help!
reply
    Bookmark Topic Watch Topic
  • New Topic