2016-02-12 48 views
-3

在過去的幾周裏,我一直在爲我的一個類使用Java。保持分數

上週我們不得不開發一款搖滾,紙,剪刀遊戲,現在我們必須跟蹤並顯示得分。

我遇到了一些麻煩,所以我希望有人能幫我找到我的錯誤。

用戶的分數只保存在一種情況下:當用戶1輸入R並且用戶2輸入S時,但是對於其他任何情況,分數不會更新。

// This is a program designed for two users to play rock, paper, scissors. 

import java.util.Scanner; 
public class GameProgram { 
    public static void main(String[] args) { 

     Scanner scan = new Scanner(System.in); 
     String user1; // The first user 
     String user2; // The second user 
     int user1score = 0; 
     int user2score = 0; 

     do { 
      do {  
       System.out.println ("Let's play rock, paper, scissors!"); // intro 
       System.out.println ("Please enter 'R' for rock, 'P' for paper, and " 
         + "'S' for scissors."); // instructions for players 
       System.out.println ("Player 1, please enter your choice."); 
       user1 = scan.next(); 
       System.out.println ("Player 2, please enter your choice."); 
       user2 = scan.next(); 

       user1 = user1.toUpperCase(); 
       // these commands make the program work if a user enters r instead of R 
       user2 = user2.toUpperCase(); 

       if(user1.equals("R")||user1.equals("P")||user1.equals("S")){ 
       } else { 
        System.out.println("User 1 entered incorrectly, please try again."); 
       } 
       if(user2.equals("R")||user2.equals("P")||user2.equals("S")){ 
       } else { 
        System.out.println("User 2 entered incorrectly, please try again."); 
       } 
       // This is so that the users will know who entered incorrectly 
       if (user1.equals(user2)) { 
        System.out.println("Oh darn, it's a tie!" + " User 1: " + 
          user1score + " User 2: " + user2score); 

        // This lets the users know that they entered the same choice. 
       } 
       else if (user1.equals("R")) { // This is an IF for user 1 entering rock 
        if (user2.equals("S")) // User 2 entering scissors vs rock 
         user1score++; 
        System.out.println("Rocks break scissors. User 1 wins!!" + " User 1: " + 
          user1score + " User 2: " + user2score); 
       } 
       else if (user2.equals("P")){ // User 2 entering paper vs rock 
        if (user1.equals("R")) 
         user2score++; 
        System.out.println("Paper covers rock. User 2 wins!!" + " User 1: " + 
          user1score + " User 2: " + user2score); 
       } 
       else if (user1.equals("P")) { // This is IF for user 1 entering paper 
        if (user2.equals("S")) // User 2 entering scissors vs paper 
         user2score++; 
        System.out.println("Scissors cut paper. User 2 wins!!" + " User 1: " + 
          user1score + " User 2: " + user2score); 
       } 
       else if (user2.equals("R")) {// User 2 entering rock vs paper 
        if (user1.equals("P")) 
         user1score++; 
        System.out.println("Paper covers rock. User 1 wins!!" + " User 1: " + 
          user1score + " User 2: " + user2score); 
       } 
       else if (user1.equals("S")) { // This is an IF for user 1 entering scissors 
        if (user2.equals("P")) // User 2 entering paper vs scissors 
         user1score++; 
        System.out.println("Scissors cut paper. User 1 wins!!" + " User 1: " + 
          user1score + " User 2: " + user2score); 
       } 
       else if (user2.equals("R")) // User 2 entering rock vs scissors 
        if (user1.equals("S")) { 
         user2score++; 
        System.out.println("Rocks break scissors. User 2 wins!!" + " User 1: " + 
          user1score + " User 2: " + user2score); 
        } 
       System.out.println(""); // provides spacing between new game} 
      } while (user1score <5); 
     } while (user2score <5); 
    } 
} 
+2

請閱讀[問]併發布[mcve]。 – Tunaki

回答

0

代替嵌套的user1的測試和user2嘗試使用& & 你沒有得到你的其他 即

if (user1 = R && user2 = P){ 

}else... 

附:嘗試總是使用大括號與if語句這是一個好習慣(儘管不是必需的)。

0
  1. 嘗試在你的if子句中使用大括號。
  2. 您可以通過否定「!」輕鬆檢查錯誤輸入。 (即!true=false
  3. 而不是嵌套你的ifs,嘗試使用& &運算符進行組合。
  4. 您也可以將輸入轉換爲整數,然後對這些if-elseif-else語句進行切換。

這將幫助您對發生的事情保持良好的概述,並且您可以更輕鬆地找到您的錯誤。

注意,它也可能是聰明的創造方法,以確定哪些球員有一個給它兩個字符串作爲參數,並讓它返回一個int /布爾針對玩家更強:

/** 
* @return int 0: error, invalid input 
*    1: player1 won 
*    2: player2 won 
*    3: tie 
*/ 
public static int determineWinner(char p1, char p2) { 
    // check input validity 
    if (!(isValidInput(p1) && isValidInput(p2))) { 
     return 0; 
    } 

    // determine a tie 
    if (p1 == p2) { 
     return 3; 
    } 

    // test-sequence 
    String testSeq = "P>R>S>P"; 
    String p1p2 = p1 + ">" + p2; 

    if (testSeq.contains(p1p2)) { 
     return 1; 
    } 
    return 2; 
} 

private static boolean isValidInput(char i) { 
    if ("RPS".contains(""+i)) { 
     return true; 
    } 
    return false; 
} 

PS :注意作業不應該在這裏回答。