2013-09-23 107 views
1

程序會爲第一個猜測數字生成正確的輸出,但在用戶輸入第二個猜測後,根本沒有輸出。請幫忙!謝謝雖然循環輸出

final int number = (int)((Math.random()*99)+1); 
    int counter = 0; 
    System.out.print("Enter a guess between 1 and 100: "); 
    while (keyboard.nextInt() > number) { 
     System.out.println("Your guess was too high. Try again."); 
     counter++; 
     System.out.print("Enter a guess between 1 and 100: "); 
    } 
    while (keyboard.nextInt() < number) { 
     System.out.println("Your guess was too low. Try again."); 
     counter++; 
     System.out.print("Enter a guess between 1 and 100: "); 
    } 
    while (keyboard.nextInt() == number) { 
     System.out.println("Congratulations! Your guess was correct!"); 
     counter++; 
     System.out.println("I had chosen " + number + " as the target number."); 
     System.out.println("You guessed it in " + counter + " tries."); 
     if (counter == 1) { 
      System.out.println("That was lucky!"); 
     } 
     if (counter >= 2 || counter <= 4) { 
      System.out.println("That was amazing!"); 
     } 
     if (counter == 5 || counter == 6) { 
      System.out.println("That was good."); 
     } 
     if (counter == 7) { 
      System.out.println("That was OK."); 
     } 
     if (counter == 8 || counter == 9) { 
      System.out.println("That was not very good."); 
     } 
     if (counter >= 10) { 
      System.out.println("This just isn't your game."); 
     } 
+3

你有while循環太多。你最終的目標是什麼? – Smit

+0

爲什麼你有2 while循環? –

+0

此外,你錯過了你的代碼的開始和結束 –

回答

3

你真的需要改變你的設計模式。

一旦不能滿足條件的1 while循環,你的代碼將永遠不會回去。

您應該只有1個while循環的猜測階段。你的代碼應該是這樣的

while(someCondition) 
{ 
    int num = keyboard.nextInt() 
    if (num > number) { 
     ...  
    } 
    else if (num < number) { 
     ... 
    } 
    else if (num == number) { 
     ... 
    } 
} 
0

此塊似乎並不工作打算:

while (keyboard.nextInt() > number) { 
    System.out.println("Your guess was too high. Try again."); 
    counter++; 
    System.out.print("Enter a guess between 1 and 100: "); 
} 
while (keyboard.nextInt() < number) { 
    System.out.println("Your guess was too low. Try again."); 
    counter++; 
    System.out.print("Enter a guess between 1 and 100: "); 
} 

假設用戶輸入-1,然後101你會在接下來的循環結束。因此,你可能要改變它的東西是這樣的:

boolean retry = true; 
while (retry) { 
    counter++; 
    int n = keyboard.nextInt(); 
    if(n > number) { 
     System.out.println("Your guess was too high. Try again."); 
    } 
    else if(n < number) { 
     System.out.println("Your guess was too low. Try again."); 
    } 
    else { 
     //number found 
     retry = false; 
    } 

    if(retry) { 
     System.out.prnt("Enter a guess between 1 and 100: "); 
    } 
} 
2
  • while循環應該是if語句。
  • 應該有一個while環周圍(幾乎)所有的代碼

你的代碼應該大致是這樣的:

while(true) { 
    // prompt for input 
    // read input 
    // break from loop if input is the exit input, eg -1 
    // check input - essentially change your whiles to ifs 
} 
0

有你如何使用while語句的一些問題。

在這裏你可以找到你的代碼的正確版本:

public static void main(final String[] args) { 
    final Scanner keyboard = new Scanner(System.in); 
    final int number = (int) ((Math.random() * 99) + 1); 
    int counter = 0; 
    System.out.print("Enter a guess between 1 and 100: "); 
    int user_number; 
    do { 
     user_number = keyboard.nextInt(); 
     if (user_number > number) { 
      System.out.println("Your guess was too high. Try again."); 
      System.out.print("Enter a guess between 1 and 100: "); 
      counter++; 
     } else if (user_number < number) { 
      System.out.println("Your guess was too low. Try again."); 
      System.out.print("Enter a guess between 1 and 100: "); 
      counter++; 
     } 
    } while (user_number != number); 
    System.out.println("Congratulations! Your guess was correct!"); 
    counter++; 
    System.out.println("I had chosen " + number + " as the target number."); 
    System.out.println("You guessed it in " + counter + " tries."); 
    if (counter == 1) { 
     System.out.println("That was lucky!"); 
    } 
    if ((counter >= 2) || (counter <= 4)) { 
     System.out.println("That was amazing!"); 
    } 
    if ((counter == 5) || (counter == 6)) { 
     System.out.println("That was good."); 
    } 
    if (counter == 7) { 
     System.out.println("That was OK."); 
    } 
    if ((counter == 8) || (counter == 9)) { 
     System.out.println("That was not very good."); 
    } 
    if (counter >= 10) { 
     System.out.println("This just isn't your game."); 
    } 
} 
0
while(someCondition) 
{ 

    if (keyboard.nextInt() > number) { 

     ... 

    } else if(keyboard.nextInt() < number) { 

     ... 

    } else { 

     ... 

    } 

}