我是新來的java編程,並正在測試一些我學到的東西,以便做出一個小猜謎遊戲。它可以工作,你可以通過它,但是當你得到第二個錯誤的數字後,你不會被告知你這個數字是低還是高。這是問題的一個例子:我的程序能夠正常工作,但不會打印時它應該是
Guess a number, 1 through 100:
50
Guess higher!
75
75
Guess lower!
65
65
Guess lower!
這裏是代碼:如果塊
public static void main(String[] args) {
Random num = new Random();
Scanner scan = new Scanner(System.in);
int rand;
boolean test;
int rand2;
int guess = 0;
rand = num.nextInt(100);
System.out.println("Guess a number, 1 through 100: ");
while(test = true){
rand2 = scan.nextInt();
if(rand == rand2){
guess++;
if(guess < 19){
System.out.println("Thats the correct number! And it only took: " + guess + " tries");
}else{
System.out.println("It took you: " + guess + " tries to guess the number!");
}
}else if(rand < rand2){
System.out.println("Guess lower! ");
guess++;
rand2 = scan.nextInt();
}else if(rand > rand2){
System.out.println("Guess higher! ");
guess++;
rand2 = scan.nextInt();
}
}
}
我看到的第一個問題,你看在這兩個條件下一個int和再次在環(所以第一個的開始猜測下/更高後忽略)。 – AxelH
[什麼是調試器,它如何幫助我診斷問題?]可能的重複(http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-我診斷的 - 問題) – Raedwald