我是一個尋求一些幫助的新手編碼器。我不得不編寫這個程序,用戶猜測0到100之間的一個數字,然後告訴它是否更高或更低。以下是代碼。我的問題是,有時當我輸入一個數字時,通常99程序終止,我不知道爲什麼。我一直在查看2小時的代碼,無法弄清楚導致程序終止的原因。任何幫助,將不勝感激。爲什麼我的Eclipse程序終止?
import java.util.Scanner;
import java.util.Random;
public class Proj71 {
private static int userNumber;
private static int firstguess = 1;
private static int numguess1;
private static int numguess2;
private static int totalguess;
private static Random generator = new Random();
private static Scanner reader = new Scanner(System.in);
private static int compNumber = generator.nextInt(100);
public static void main(String[] args) {
UserGuess();
UserHighGuess();
UserLowGuess();
UserEquals();
}
private static void UserGuess() {
System.out.println("What number am I thinking of between 0 and 100?: ");
userNumber = reader.nextInt();
}
private static void UserHighGuess() {
while (userNumber > compNumber) {
System.out.println("Lower! Try again: ");
userNumber = reader.nextInt();
numguess1++;
}
}
private static void UserLowGuess() {
while (userNumber < compNumber) {
System.out.println("Higher! Try again: ");
userNumber = reader.nextInt();
numguess2++;
}
}
private static void UserEquals() {
if (userNumber == compNumber) {
totalguess = numguess1 + numguess2 + firstguess;
System.out.println("You got it!");
System.out.println("Total number of guess: " + totalguess);
}
}
}
感謝, Jmanlikescake(抱歉,如果這個職位是非常糟糕)
您是否試過在調試器中單步執行代碼? – rrirower
這看起來像java,如果你想讓合適的人看到它,你應該這樣標記它(我繼續爲你做)。 – Josh