Note that there are some explanatory texts on larger screens.

plurals
  1. POI need help debugging a java guessing game app
    primarykey
    data
    text
    <p>I made this guessing game, but I found a bug in it and I don't know how to fix it. Maybe you guys can help?</p> <p>the problem is, is that i will enter ONLY 9 wrong numbers, but then it will tell me I guessed 10 times. Which I didn't. </p> <p>If you find any other bugs, could you tell me how to fix those too? AND if you can tell me any tips/techniques that I could use next time that would be great.</p> <pre><code>import java.util.Scanner; public class main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int number_to_guess = 3; int guess; //int number_of_guesses = 0; Scanner input = new Scanner(System.in); System.out.println("Hello, please enter a number between 1 and 10. You only have 10 guesses, so be be smart!!"); for(int number_of_guesses = 1; number_of_guesses&lt;=10;){ guess = input.nextInt(); if (guess &gt;=1 &amp;&amp; guess &lt;=10){//is it between 1 and 10 if(guess == number_to_guess){//is it the right number number_of_guesses++;//increase guess number System.out.println("Congradulations!!! You guessed the number in " + number_of_guesses + " tries"); break;//exit the loop } else{//if its not the right number, then try again System.out.println("I'm sorry. Thats not it. Please try again."); number_of_guesses++; if (number_of_guesses == 10){ System.out.println("I'm sorry, but it appears you have guessed 10 times and didn't get the right number. The number was " + number_to_guess); break; } } } else{//invalid number entry System.out.println("I'm sorry, but your number that you have enter is invalid. Please make sure it is between 1 and 10"); number_of_guesses--;//the count will go up, even though the guessed an invalid number, so set it back to what it was before } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload