2017-09-29 38 views
0

我正在試圖製作一款石頭剪刀sp l蜥蜴遊戲。我一直在學習if/else語句。我試圖做到這一點,如果player1輸入一個無效的命令,那麼它會打印出「對不起,這不是一個有效的命令」像else語句。如果兩個玩家輸入一個命令並且一個是錯誤的,但它不起作用,但是如果玩家1沒有輸入命令則可以。你能幫我弄清楚我應該做什麼嗎? 這裏是我的代碼:我該如何做到這一點,如果第一個玩家輸入錯誤的命令,那麼它會有一個特定的提示?

package csc212hw03; 
    import java.util.Scanner; 

    public class Main { 

public static void main(String[] args) { 
    String player1; 
    String player2; 
    String player1Choice; 
    String player2Choice; 
    String line; 

      // 「1」 for Paper 
    //「2」 for Rock 
    //「3」 for Spock 
    //「4」 for Lizard 
    //「5」for Scissors 
    Scanner kb = new Scanner(System.in); 
    System.out.println("Player 1, please enter your name:"); 
    player1 = kb.nextLine(); 

    System.out.println("Player 2, please enter your name:"); 
    player2 = kb.nextLine(); 

    System.out.println(player1 + ", please enter your command:"); 
    player1Choice = kb.nextLine(); 

    System.out.println(player2 + ", please enter your command:"); 
    player2Choice = kb.nextLine(); 

    if (player1Choice.equals("1") && player2Choice.equals("2")) { 
     System.out.println(player1 + " wins! Paper covers Rock."); 
     System.out.println("Thank you for playing."); 
    } else if (player2Choice.equals("2") && player1Choice.equals("1")){ 
     System.out.println(player2 + " wins! Paper covers Rock."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("1")&& player2Choice.equals("1")) { 
     System.out.println("Draw!"); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("2")&& player2Choice.equals("2")) { 
     System.out.println("Draw!"); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("1")&& player2Choice.equals("3")) { 
     System.out.println(player1 + " wins! Paper disproves Spock."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("3") && player2Choice.equals("1")) { 
     System.out.println(player2 + " wins! Paper disproves Spock."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("3")&& player2Choice.equals("3")) { 
     System.out.println("Draw!"); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("2") && player2Choice.equals("3")) { 
     System.out.println(player2 + " wins! Spock vaporizes Rock."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("3") && player2Choice.equals("2")) { 
     System.out.println(player1 + " wins! Spock vaporizes Rock."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("3") && player2Choice.equals("5")) { 
     System.out.println(player1 + " wins! Spock smashes Scissors."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("5") && player2Choice.equals("3")) { 
     System.out.println(player2 + " wins! Spock smashes Scissors."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("5") && player2Choice.equals("5")) { 
     System.out.println("Draw!"); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("2") && player2Choice.equals("5")) { 
     System.out.println(player1 + " wins! Rock crushes Scissors."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("5") && player2Choice.equals("2")) { 
     System.out.println(player2 + " wins! Rock crushes Scissors."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("2") && player2Choice.equals("4")) { 
     System.out.println(player1 + " wins! Rock crushes Lizard."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("4") && player2Choice.equals("2")) { 
     System.out.println(player2 + " wins! Rock crushes Lizard."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("3") && player2Choice.equals("4")) { 
     System.out.println(player2 + " wins! Lizard poisons Spock."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("4") && player2Choice.equals("3")) { 
     System.out.println(player1 + " wins! Lizard poisons Spock."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("5") && player2Choice.equals("1")) { 
     System.out.println(player1 + " wins! Scissors cuts Paper."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("1") && player2Choice.equals("5")) { 
     System.out.println(player2 + " wins! Scissors cuts Paper."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("5") && player2Choice.equals("4")) { 
     System.out.println(player1 + " wins! Scissors decpitates Lizard."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("4") && player2Choice.equals("5")) { 
     System.out.println(player2 + " wins! Scissors decapitates Lizard."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("5") && player2Choice.equals("5")) { 
     System.out.println("Draw!"); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("4") && player2Choice.equals("4")) { 
     System.out.println("Draw!"); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("4") && player2Choice.equals("1")) { 
     System.out.println(player1 + " wins! Lizard eats Paper."); 
     System.out.println("Thank you for playing."); 
    } else if (player1Choice.equals("1") && player2Choice.equals("4")) { 
     System.out.println(player2 + " wins! Lizard eats Paper."); 
     System.out.println("Thank you for playing."); 
    } else { 
     System.out.println("I'm sorry, this is not a valid command."); 
     System.out.println("Thank you for playing."); 

    } 
     } 
    } 

回答

0
 while (true) { 
      Scanner kb = new Scanner(System.in); 
      System.out.println(player1 + ", please enter your command:"); 
      String player1Choice = kb.nextLine(); 

      if (!player1Choice.equals("1") || !player1Choice.equals("2") || !player1Choice.equals("3") 
        || !player1Choice.equals("4") || !player1.equals("5")) { 
       System.out.println("Sorry, this is not a valid command"); 
      } else { 
       break; 
      } 
     } 

此外,您還可以在if語句想到更好的方法。

0

而不是隻要求玩家1的選擇一次,把它放在一個while循環詢問,直到輸入一個有效的響應,這樣

player1Choice = ""; 
boolean player1ChoiceValid = false; 
while(!player1ChoiceValid){ 
    System.out.println(player1 + ", please enter your command:"); 
    player1Choice = kb.nextLine(); 
    if(player1Choice.equals("1") 
      || player1Choice.equals("2") 
      || player1Choice.equals("3") 
      || player1Choice.equals("4") 
      || player1Choice.equals("5")){ 
     player1ChoiceValid = true; 
    }else{ 
     System.out.println("I'm sorry, this is not a valid command."); 
    } 
} 

這將持續循環的問題,直到輸入有效,這意味着它是1或2或3或4或5.這也應該爲玩家2完成。

相關問題