2015-09-16 103 views
0

如何輸入錯誤的輸入(除了選項,岩石,紙張,剪刀之外的東西),讓電腦自動獲勝?它也需要這些數據。另外,當玩家再次選擇再玩時,我怎樣才能消除「歡迎...」的重現;只有在程序第一次啓動時才需要。岩石紙剪刀遊戲最好3出3循環問題

import java.util.Scanner; 
import java.util.Random; 
/** 
* 
* @author Chloe Harris 
* 
*/ 
public class RockPaperScissorsGame { 

    public static void main(String[] args) { 
     // TODO code application logic here 

     //Set integers for wins, losses, rounds, and user 
     while(true){ 
      int wins = 0; 
      int losses = 0; 
      int round = 0; 
      int Player = 0; 

      //Plays 3 rounds before terminating 
      //Prompt user to input Rock Paper Scissors 
      System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n"); 

      Scanner keyboard = new Scanner (System.in); 
      while(round<3) { 
       System.out.println("Enter \"Rock\", \"Paper\" or \"Scissors\""); 
       Random Game = new Random(); 
       int Computer = 1+Game.nextInt(3); 

       int Scissors, Rock, Paper; 
       Rock = 1; 
       Paper = 2; 
       Scissors= 3; 

       String UserInput = keyboard.next(); 
       if(UserInput.equals("Rock")) { 
        Player = 1; 
       } 
       if(UserInput.equals("Paper")) { 
        Player = 2; 
       } 
       if(UserInput.equals("Scissors")) { 
        Player = 3; 
       } 

       //If the user enters a value greater then 3 (Scissors) or less than 1 (Rock) 
       //it will terminate the program and display an error message 
       while (Player > 3 || Player < 1) { 
        losses++; 
        round++; 
        System.out.println("Not a valid input! Computer wins"); 
        System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times"); 

       } 

       //Establish tie scenarios using if statements 
       if(Player == Computer){ 
        if(Player == Scissors){ 
         System.out.println("Scissors v Scissors! Tie!"); 
         round++; 
        } 
        if(Player == Rock){ 
         System.out.println("Rock v Rock! Tie!"); 
         round++; 
        } 
        if(Player == Paper){ 
         System.out.println("Paper v Paper! Tie!"); 
         round++; 
        } 
        System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times"); 

       } 

       //Establish the various winning scenarios using if and else if statements 
       //Player wins 
       if(Player == Scissors) 
        if(Computer == Paper){ 
         System.out.println("Scissors v Paper! Player Wins!"); 
         wins++; 
         round++; 
         System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times"); 
        } 
       //Computer wins 
        else if(Computer == Rock){ 
         System.out.println("Scissors v Rock! Computer Wins!"); 
         losses++; 
         round++; 
         System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times"); 
        } 
       //Player wins 
       if(Player == Rock) 
        if(Computer == Scissors){ 
         System.out.println("Rock v Scissors! Player Wins!"); 
         wins++; 
         round++; 
         System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times"); 
        } 
       //Computer wins 
        else if (Computer == Paper){ 
         System.out.println("Rock v Paper! Computer Wins!"); 
         losses++; 
         round++; 
         System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times"); 
        } 
       //Player Wins 
       if(Player == Paper) 
        if(Computer == Rock){ 
         System.out.println("Paper v Rock! Player Wins!"); 
         wins++; 
         round++; 
         System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times"); 

        } 
       //Computer Wins 
        else if (Computer == Scissors){ 
         System.out.println("Paper v Scissors! Computer Wins!"); 
         losses++; 
         round++; 
         System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times"); 
        } 
      } 

      //Determine final winner using if statements 
      //Ask if player would like to play again by setting up string 
      if(wins>losses){ 
       System.out.println("The Player Wins!"); 
      }if(losses>wins){ 
       System.out.println("The Computer Wins!"); 
      } 
      System.out.println("Play again? \"Yes\" or \"No\""); 
      Scanner YesNo = new Scanner(System.in); 

      String YesNo_String = YesNo.next(); 
      if(YesNo_String.equalsIgnoreCase("yes")) { 
      }if(YesNo_String.equalsIgnoreCase("No")) { 
       System.out.println ("Goodbye!"); 
      } 
     } 
    } 
} 
+0

順便說一句,你知道我們只用3行代碼就可以確定誰是贏家嗎?我看到,爲了確定誰是勝利者,你寫了30多行代碼。 – user3437460

回答

1

你總是可以只改變你的if語句如下:

if(UserInput.equals("Rock")) { 
    Player = 1; 
} 
else if(UserInput.equals("Paper")) { 
    Player = 2; 
} 
else if(UserInput.equals("Scissors")) { 
    Player = 3; 
} 
else { 
    Player = 0; 
} 

你已經擁有它設置,使玩家如果1 <,計算機自動獲勝。

注意:您的while (Player > 3 || Player < 1)聲明實際上應該是if聲明。否則,你將有一個無限循環。

要回答第二個問題,只需將語句System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");移到循環上方即可。這樣它只會被問到一次:在開始時。

0

對於玩家輸入錯誤的數據並檢查他們是否已經玩過,創建布爾值並在if/else語句中檢查這些布爾值。由於這可能或可能不是功課,我將爲「歡迎...」消息解決這個問題。

public static void main(String[] args) { 
    // TODO code application logic here 
    Boolean firstPlay = true; 

    //Set integers for wins, losses, rounds, and user 
    while(true){ 
     int wins = 0; 
     int losses = 0; 
     int round = 0; 
     int Player = 0; 

     //Plays 3 rounds before terminating 
     //Prompt user to input Rock Paper Scissors 
     if(firstPlay) { 
      System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n"); 
      firstPlay = false; 
     } 
0

好像你是「玩家」的值設置爲是1,2,或3,缺省爲0。我會說,它設置爲默認值在每個循環的開始,和如果它的0讓電腦贏。或者將你的if語句改爲if-elseif。我會在下面提供一個例子。

.... 
if(UserInput.equals("Rock")) { 
    Player = 1;  
} else if(UserInput.equals("Paper")) { 
    Player = 2; 
} else if(UserInput.equals("Scissors")) { 
    Player = 3; 
} else { 
    Player = 0; 
} 

既然你已經有了一份聲明中說,如果球員< 1> 3,電腦贏了,你設置。您的「歡迎」聲明也在您的while循環中。因爲每次迭代循環都會看到它。嘗試在循環之外移動它。