2013-10-31 299 views
6

問候堆棧溢出用戶,我今天晚上向你求助,幫助我創建了一個Java程序。我對Java比較陌生,所以請原諒我對這個話題的無知。我做了一個Java程序,這是一個「搖滾」「紙」「剪刀」遊戲,似乎有一個錯誤的陳述之一。Java - 嵌套的while循環

import java.util.Scanner; 

public class TheAntlers { 
public static void main(String[] args) { 

    int playerHumanWins = 0; 
    int playerComputerWins = 0; 
    int numberOfTies = 0; 
    int computerResult; 

    Scanner input = new Scanner(System.in); 

    while(true) { 

     String startGame; 
     String playerHuman; 
     String playerComputer = " "; 

     System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): "); 
     startGame = input.nextLine(); 

     startGame = startGame.toUpperCase(); 

     if(startGame.equals("N")) { 
      System.out.println("NO!"); 
      break; 
     } 
     else if(! startGame.equals("Y")) { 
      startGame = startGame.toLowerCase(); 
      System.out.println("Sorry, " + startGame + " is not a valid entry...");    
     } 
     while(startGame.equals("Y")) { 
      System.out.print("Please choose \"Rock\", \"Paper\", or \"Scissors\": "); 
      playerHuman = input.nextLine(); 

      computerResult = (int)(Math.random() * 3); 

      playerHuman = playerHuman.toUpperCase(); 

      if(computerResult == 1) { 
       playerComputer = "ROCK"; 
      } 
      else if(computerResult == 2) { 
       playerComputer = "PAPER"; 
      } 
      else if (computerResult == 3) { 
       playerComputer = "SCISSORS"; 
      } 

      switch (playerHuman) { 
       case "ROCK" : 
        if(playerComputer.equals(playerHuman)) { 
        System.out.println("Tie you both picked \"ROCK\""); 
        numberOfTies++; 
       } 
        else if(playerComputer.equals("PAPER")) { 
         System.out.println("Computer wins!"); 
         playerComputerWins++; 
        } 
        else { 
         System.out.println("You win, \"ROCK\" beats " + "\"" + playerComputer + "\""); 
         playerHumanWins++; 
         return; 
        } 
        break; 
       case "PAPER" : 
        if(playerComputer.equals(playerHuman)) { 
        System.out.println("Tie you both picked \"PAPER\""); 
        numberOfTies++; 
       } 
        else if(playerComputer.equals("ROCK")) { 
         System.out.println("You win, \"PAPER\" beats " + "\"" + playerComputer + "\""); 
         playerHumanWins++; 
         return; 
        } 
        else { 
         System.out.println("Sorry, the computer won!"); 
         playerComputerWins++; 
        } 
        break; 
       case "SCISSORS" : 
        if(playerComputer.equals(playerHuman)) { 
        System.out.println("Tie you both picked \"SCISSORS\""); 
        numberOfTies++; 
       } 
        else if(playerComputer.equals("PAPER")) { 
         System.out.println("You win, \"SCISSORS\" beats " + "\"" + playerComputer + "\""); 
         playerHumanWins++; 
         return; 
        } 
        else { 
         System.out.println("Sorry, the computer won!"); 
         playerComputerWins++; 
        } 
        break; 
       default: 
        playerHuman = playerHuman.toLowerCase(); 
        System.out.println("Sorry, " + playerHuman + " is not a valid entry..."); 
        break; 
      } 
     } 
    }   
} 
} 

我面臨的問題與獲獎計算有關。當我運行該程序,我多次進入岩石,直到我贏了,輸出會你贏了,「ROCK」節拍「」,但與任何其他選項,我得到你贏了,「ROCK」擊敗「紙」

我的問題是,爲什麼我在玩搖滾時會得到空回調?

* 此外,如果你願意指出任何其他建議,以幫助新手,這將是偉大的。 *

回答

5

Math.random() * 3是一個數字0以上且小於3。

它轉換成一整型後,它是0,1或2。

 if(computerResult == 0) { 
      playerComputer = "ROCK"; 
     } 
     else if(computerResult == 1) { 
      playerComputer = "PAPER"; 
     } 
     else if (computerResult == 2) { 
      playerComputer = "SCISSORS"; 
     } 

建議:

簡明扼要。你可以改變

String startGame; 
startGame = input.nextLine(); 
startGame = startGame.toUpperCase(); 

String startGame = input.nextLine().toUpperCase(); 

當你沒有滾動,滾動它更具可讀性。

另外,知道equalsIgnoreCase()存在。

+0

更好的是if(computerResult == 0)playerComputer =「ROCK」; (computerResult == 1){ } playerComputer =「PAPER」; } else { playerComputer =「SCISSORS」; } –

+2

不要忘記添加,如果你想要一個1到3之間的數字,你可以這樣做:1 +(int)Math.random()* 3; –

+0

好點@AmirAfghani,雖然在這種情況下,0,1和2對大多數程序員來說是很自然的。 –

0

您會發現(int)(Math.random()* 3)有時會產生0,從不會產生3,這是給您的空白,因爲您沒有0的結果。

具體來說,其時的Math.random()返回的值小於0.33

1

這不是一個完整的新手,但我會使用此代碼的遊戲模式:

enum Choice { ROCK, PAPER, SCISSORS } 

enum Result { COMPUTER_WINS, TIE, HUMAN_WINS } 

Result decide(Choice computer, Choice human) { 
    if (human == computer) { 
    return Result.TIE; 
    } else if (…) { 
    … 
    } 
} 

你有一些這樣處理遊戲本身的代碼的一部分,而其他一些代碼處理用戶交互。