-1

この推測ゲームを作成しましたが、バグが見つかりました。修正方法がわかりません。多分あなたたちは助けることができますか?

問題は、間違った数字を 9 つしか入力しないのに、10 回も当てたということです。私はしませんでした。

他のバグを見つけた場合は、それらの修正方法も教えていただけますか? また、次回使用できるヒントやテクニックを教えていただければ幸いです。

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<=10;){
        guess = input.nextInt();
        if (guess >=1 && guess <=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
        }
    }



}

}

4

1 に答える 1