2014-01-27 45 views
1

當我運行這個時,它顯示案例中的所有System.out.println("")陳述。我如何使它選擇正確的if聲明,而不是別的?Rock Paper Scissor如果陳述不起作用

如果在找到結果後我可以回到起點,那也是有幫助的。

import java.util.Scanner; 
import java.util.Random; 

public class TEST { 
private static Scanner myScanner; 

public static void main(String[] args) { 
    myScanner = new Scanner(System.in); 
    Random myRandom = new Random(); 
    int randomNumber; 
    char reply; 
    System.out.print("Rock(R) Paper(P) or Scissors(S)? "); 
    reply = myScanner.findWithinHorizon(".", 0).charAt(0); 
    randomNumber = myRandom.nextInt(3) + 1; 
    switch (randomNumber) { 
    case 1: 
      if (reply == 'S' || reply == 's'); { 
      System.out.println("Computer: Rock. You lost!"); 
      } 
      if (reply == 'R' || reply == 'r'); { 
      System.out.println("Computer: Rock. You tied!"); 
      } 
      if (reply == 'P' || reply == 'p'); { 
      System.out.println("Computer: Rock. You won!"); 
      break; 
      } 
    case 2: 
      if (reply == 'P' || reply == 'p'); { 
      System.out.println("Computer: Paper. You tied!"); 
      } 
      if (reply == 'S' || reply == 's'); { 
      System.out.println("Computer: Paper. You won!"); 
      } 
      if (reply == 'R' || reply == 'r'); { 
      System.out.println("Computer: Paper. You lost!"); 
      break; 
      } 
    case 3: 
      if (reply == 'R' || reply == 'r'); { 
      System.out.println("Computer: Scissor. You won!"); 
      } 
      if (reply == 'P' || reply == 'p'); { 
      System.out.println("Computer: Scissor. You lost!"); 
      } 
      if (reply == 'S' || reply == 's'); { 
      System.out.println("Computer: Scissor. You tied!"); 
      break; 
      }    
     } 
    } 
} 
+2

**獎勵練習:**想想如何進一步簡化程序,避免不必要的重複。使用'Character.toLowerCase',並將數值分配給用戶的輸入,以便像減法這樣的操作可以幫助您輕鬆確定贏/失狀態。 – ADTC

回答

5

你有兩個問題:

1)您的if語句不會運行,因爲它們以分號結尾。這意味着{}中的代碼形成一個塊並以任一方式運行。

2)您的休息時間不對。如果選擇'R'(或'r'),第一個只會被打破。如果案件運行,後者會中斷。

if (reply == 'P' || reply == 'p'); { 
    System.out.println("Computer: Paper. You tied!"); 
    } 
    if (reply == 'S' || reply == 's'); { 
    System.out.println("Computer: Paper. You won!"); 
    } 
    if (reply == 'R' || reply == 'r'); { 
    System.out.println("Computer: Paper. You lost!"); 
    break; 
    } 

VS

if (reply == 'P' || reply == 'p') { 
    System.out.println("Computer: Paper. You tied!"); 
    } 
    if (reply == 'S' || reply == 's') { 
    System.out.println("Computer: Paper. You won!"); 
    } 
    if (reply == 'R' || reply == 'r') { 
    System.out.println("Computer: Paper. You lost!"); 
    } 
    break; 

而且,在意見提出,你可以使用一個循環以提示輸入。例如:

while (! done) { 
    System.out.print("Rock(R) Paper(P) or Scissors(S)? (or Quit(Q) "); 
    reply = myScanner.findWithinHorizon(".", 0).charAt(0); 
    // more code here 
} 

您會在這裏需要另一種情況下,您可以設置做到真實,結束循環。

+0

也可以添加一個關於添加'while'循環來重複程序的註釋,直到用戶輸入類似'X'的東西來退出。 (OP問這個) – ADTC

+0

@ADTC:好主意。添加 –

4

你跟着如果與一個額外的;

的語法的if語句是:

if(condition){ 
    Expressions; 
} 

分號使它:

if(condition) 
    ; //empty line, effectively ignores the if 
{ 
    Expressions; 
}