我正在做隨機數發生器,然後讓用戶猜測數字。然後計算機會告訴用戶該號碼是否正確以及猜測距離該號碼有多遠。當我編譯:隨機數發生器獲取錯誤++ ++
public static void guessNumber()
{
//Asks for guess of the number. Prints whether the guess is above or below randomNumber.
Scanner reader = new Scanner(System.in);
numberOfTries = 0;
while(userGuess != randomNumber){
System.out.println("Guess the number ");
int userGuess = reader.nextInt();
if (userGuess == randomNumber) {
int numberOfGuesses++;
System.out.println("Correct! It took you " + numberOfTries + " tries to find my numnber");
} else {
int distanceFromNumber = Math.abs(randomNumber - userGuess);
int numberOfGuesses++;
System.out.println("Incorrect! The number that you guessed is " + distanceFromNumber + "numbers away from my number ");
}
}
我得到這個錯誤:
C:\!!VHSJava\Projects\random.java:47: error: ';' expected
int numberOfGuesses++;
^
C:\!!VHSJava\Projects\random.java:51: error: ';' expected
int numberOfGuesses++;
^
我試着將其更改爲numberOfGuesses + = 1,但得到了同樣的錯誤。
這個問題似乎是偏離主題,因爲它是關於遞增的變量而宣告它。 – devnull
@devnull我不明白這是如何讓它脫離主題。不要說這是一個很好的問題,或者顯示出大量的研究成果。這似乎是新手,而不是題外話,對嗎? – Gray
@格雷怎麼樣__最小的理解___原因? – devnull