2014-10-27 25 views
0

每當我運行它,似乎循環繼續播放工作,但只要conputerChoose執行randomGenerator,遊戲結果就不會正確輸出。請幫忙。我是新來的java,我們只能使用3種方法 - 說明,playGame和computerChoose。我們也假設使用用戶控制的循環來繼續工作。我似乎無法得到這個權利,我仍然需要添加一個循環來計算遊戲已經播放的次數,贏得的次數和計算機贏得的次數。我無法讓我的搖滾,紙張,剪刀程序讀取正確的結果

import java.util.*; 

public class PRS { 

    public static Scanner kbd = new Scanner(System.in); 

    public static void instructions() { 

     System.out.println("\nThis is the popular game of paper, rock, scissors. Enter your" 
       + "\nchoice by typing the word \"paper\", the word \"rock\" or the word" 
       + "\n\"scissors\". The computer will also make a choice from the three" 
       + "\noptions. After you have entered your choice, the winner of the" 
       + "\ngame will be determined according to the following rules:" 
       + "\n\nPaper wraps rock (paper wins)" 
       + "\nRock breaks scissors (rock wins)" 
       + "\nScissors cuts paper (scissors wins)" 
       + "\n\nIf both you and the computer enter the same choice, then the game " 
       + "\nis tied.\n"); 

    } 

    public static int playGame(){ 

     int outcome = 0; 
     System.out.print("Enter your choice: "); 
     kbd = new Scanner(System.in); 
     String player = kbd.nextLine().toLowerCase(); 
     String computerChoice = computerChoose(); 
     System.out.println("\nYou entered: " + player); 
     System.out.println("Computer Chose: " + computerChoose()); 

     if(player.equals(computerChoose())){ 

      outcome = 3; 


     } 

     else if (player.equals("paper") && computerChoice.equals("rock")){ 

      outcome = 1; 

     } 
     else if (computerChoice.equals("paper") && player.equals("rock")){ 

      outcome = 2; 
     } 
     else if (player.equals("rock") && computerChoice.equals("scissors")){ 


      outcome = 1; 


     } 
     else if (computerChoice.equals("rock") && player.equals("scissors")){ 
      outcome = 2; 

     } 

     else if (player.equals("scissors") && computerChoice.equals("paper")){ 

      outcome = 1; 
     } 

     else if (computerChoice.equals("scissors") && player.equals("paper")){ 

      outcome = 2; 
     } 

     else if (player.equals("rock") && computerChoice.equals("paper")){ 

      outcome = 2; 
     } 

     else if (computerChoice.equals("rock") && player.equals("paper")){ 

      outcome = 1; 
     } 
     return outcome; 

    } 


    public static String computerChoose(){ 

     /*return "scissors";*/ 
     Random generator = new Random(); 
     String [] answer = new String [3]; 
     answer [0]= "paper"; 
     answer [1] = "rock"; 
     answer [2] = "scissors"; 
     return answer[generator.nextInt(3)]; 

    } 

    public static void main(String[] args) { 


     kbd = new Scanner(System.in); 

     System.out.println("THE GAME OF PAPER, ROCK, SCISSORS:"); 

     System.out.print("\nDo you need instructions (Y or N)? "); 

     String userPlay = kbd.nextLine(); 

     if (userPlay.equalsIgnoreCase("y")){ 
      instructions(); 
     } 




     String answer; 
     do{ 

      int result = playGame(); 

      System.out.println(result); 
      switch (result){ 
      case 1: 
       System.out.println("YOU WIN!"); 
       break; 
      case 2: 
       System.out.println("Comp WINs!"); 
       break; 
      case 3: 
       System.out.println("IT'S A TIE!"); 
       break; 
      default: 
      } 

      System.out.print("\nPlay again (Y or N)? "); 
      answer = kbd.nextLine(); 

     }while(answer.equalsIgnoreCase("y")); 


    } 


} 

回答

4

您需要做的第一件事情只需撥打computerChoose()一次。每次你調用這個方法時,它都會生成一個新的隨機數,因此會產生一個不同的答案。您只應在playGame()內調用一次,並將其分配給本地變量。

E.g.

String computerChoice = computerChoose(); 

然後用這個變量名替換你所有的其他呼叫到computerChoose()。這樣您將顯示一個值並僅比較邏輯中的一個值。對於跟蹤其他信息,如遊戲數量和贏/輸數量,考慮再聲明幾個類變量(或主方法中的局部變量),然後可以分配,增加和讀取。你可以在主要方法的do-while循環中完成所有這些。不需要任何額外的循環。

+0

非常感謝。但是當我被問及是否想繼續時,我仍然得到一個奇怪的迴應。無論結果如何,它都能保持與最後一個結果相同的結果。 – mlopman 2014-10-27 03:23:30

+0

查看@ArmaAK下面的答案,他拿起了我在答案中錯過的內容。 – 2014-10-27 03:25:34

1

除了Adam的回答,最後將do-while循環更改爲以下內容將解決幾個不同的問題。

String answer; 
int winCount=0, lossCount=0, tieCount=0; 
do{ 
    int result = playGame(); 

    switch (result){ 
    case 1: 
     System.out.println("YOU WIN!"); 
     winCount++; 
     break; 
    case 2: 
     System.out.println("Comp WINs!"); 
     lossCount++; 
     break; 
    case 3: 
     System.out.println("IT'S A TIE!"); 
     tieCount++; 
     break; 
    default: 
    } 

    System.out.print("\nPlay again (Y or N)? "); 
    answer = kbd.nextLine(); 

}while(answer.equalsIgnoreCase("y")); 
System.out.printf("Wins: %d, Losses: %d, Total plays: %d%n", winCount, lossCount, winCount+lossCount+tieCount); 

您需要更新result while循環,否則裏面只有第一場比賽的結果將是準確的。

+0

謝謝。 IT在一定程度上起作用。但由於某種原因,它現在正在結果行之前打印一個數字。另外,當我放入紙張時,第二個環繞它打印0。 – mlopman 2014-10-27 03:28:04

+0

這是我的輸出到目前爲止: – mlopman 2014-10-27 03:29:15

+0

對不起,這是我的調試。在playGame()' – ArmaAK 2014-10-27 03:29:38

相關問題