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?
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.
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.
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.
posted 2 weeks ago
Ahhhh, so I should swap the if statement and the for loop and also change the middle condition to just and equals?
posted 2 weeks ago
Actually maybe swap from the if statement to a while loop that runs while the word doesn’t equal blue.
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.