2012-10-23 89 views
0

我是新來編程,我一直在試圖創建一個簡單的岩石紙剪刀遊戲。基本上,它使用一個while循環並詢問用戶他們是否想要玩(或繼續)。一旦他們不再想要,該計劃必須打印出遊戲總數,勝率,失敗次數和勝率。我已經完成了整個程序的工作,除非它總是說勝利的百分比是0.0%,即使它不是。我已經使用了一個if語句來避免零錯誤的分隔。我沒有得到任何運行時或編譯器錯誤,所以我要麼丟失了一些東西,要麼出現一個我無法找到的邏輯錯誤。我想繼續使用掃描儀。岩石紙剪刀遊戲

import java.util.Scanner; 

public class RockPaperScissors { 

/* 
* Program allows user to play Rock, Paper and Scissors as many times as desired by entering Y until they enter N. 
* Program will print amount of games played, amount lost, amount won and percentage won. 
* User must enter "Y", "N", "Rock", "Paper" or "Scissors" with correct capitalization and spelling. 
*/ 

public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    int playerWins = 0; 
    int compWins = 0; 
    int gamesPlayed = 0; 

    while (true) { 
     System.out.println("Do you want to play Rock Paper Scissors (Y/N): "); 
     String play = input.nextLine(); 

     // user terminates game and program prints number of wins, losses and percentage of wins. 
     if (play.equals("N")) { 

      System.out.println("You played a total of " + gamesPlayed + " matches against the computer"); 
      System.out.println("The computer won " + compWins + " matches"); 
      System.out.println("You won " + playerWins + " matches"); 

      // 0% wins when no games are played. 
      if (gamesPlayed == 0) { 
       System.out.println("You won 0% of the time!"); 
       break; 

      } else if (gamesPlayed > 0) { 
       double totalWins = (int)(playerWins/gamesPlayed) * 100; 
       System.out.println("You won " + totalWins + "% of the time!"); 
       break; 
      } 

     } else if ((!play.equals("N")) && (!play.equals("Y"))) { 
      System.out.println("Invalid entry"); 
     } else { 

      System.out.println("Welcome to Rock, Paper and Scissors!"); 
      System.out.print("Select \"Paper\", \"Rock\" or \"Scissors\": "); 
      String decision = input.nextLine(); 
      System.out.println("Your selection: " + decision); 

      // random number generator producing integer values between 1 to 3 for computer's choices. 
      // 1 is for Rock, 2 is for Paper and 3 is for Scissors. 
      int num = (int)(Math.random() * (3-0) + 1); 

      switch (num) { 

       // Computer picks Rock 
       case 1: 
        if (decision.equals("Rock")) { 
        System.out.println("Tie, you and the computer selected rock"); 
        gamesPlayed++; 
       } else if (decision.equals("Paper")) { 
        System.out.println("You win, paper beats rock!"); 
        gamesPlayed++; 
        playerWins++; 
       } else if (decision.equals("Scissors")) { 
        System.out.println("Computer wins, rock beats scissors!"); 
        gamesPlayed++; 
        compWins++; 
       } else { 
        System.out.println(decision + " is not a valid input"); 
       } 
       break; 
       case 2: 
        // computer picks Paper 
        if (decision.equals("Rock")) { 
        System.out.println("Computer wins, rock beats paper!"); 
        gamesPlayed++; 
        compWins++; 
       } else if (decision.equals("Paper")) { 
        System.out.println("Tie, you and the computer selected paper"); 
        gamesPlayed++; 
       } else if (decision.equals("Scissors")) { 
        System.out.println("You win, scissors beats paper"); 
        gamesPlayed++; 
        playerWins++; 
       } else { 
        System.out.println(decision + " is not a valid input"); 
       } 
       break; 
       case 3: 
        // computer picks Scissors 
        if (decision.equals("Rock")) { 
        System.out.println("You win, rock beats scissors"); 
        gamesPlayed++; 
        playerWins++; 
       } else if (decision.equals("Paper")) { 
        System.out.println("Computer wins, scissors beats paper"); 
        gamesPlayed++; 
        compWins++; 
       } else if (decision.equals("Scissors")) { 
        System.out.println("Tie, you and the computer selected scissors"); 
        gamesPlayed++; 
       } else { 
        System.out.println(decision + " is not a valid input"); 
       } 
       break; 

      } 
     } 

    } 

} 

}

+4

錯誤是在這裏:'(INT)(playerWins/gamesPlayed)* 100;'。提示:整數除法。 – nhahtdh

+0

有關R-P-S的枚舉解決方案的示例(也使用雙贏陣列),請看[這裏](http://stackoverflow.com/a/8264256/522444) –

回答

5

的問題是在double totalWins = (int)(playerWins/gamesPlayed) * 100;。由於playerWinsgamesPlayed都是整數類型(特別是int類型),所以Java正在執行「整數除法」,它返回除法的商作爲結果並忽略餘數。因此,要防止它這樣做,你最好改變該行:

double totalWins = (playerWins * 100.0)/gamesPlayed; 
//     /------------------\ 
// This converts the `playerWins` to a `double` and does the division as you expect 
+0

謝謝,這解決了問題!我有點困惑,爲什麼這個解決方案的工作,而不是顯式鑄造2變量加倍? –

+0

顯式轉換爲double的問題在於,您將得到類似於「0.8」的結果,然後將其轉換爲給出「0」的整數(丟失小數部分)。然後你乘以'100',它給出'0'。在這個方法中,這個方法給出了(在這個例子中)'(800.0)/(10)',並且Java執行了分割,它返回了它將會轉換爲80的一個整數。 – Lee

+0

現在我想到了它。謝謝! –