保持分数

时间:2016-02-12 13:03:56

标签: java

在过去的几周里,我一直在为我的一个班级使用Java。

上周我们不得不开发摇滚,纸张,剪刀游戏,现在我们必须跟踪并显示得分。

我遇到了一些麻烦,所以我希望有人可以帮我找到我的错误。

用户的得分仅保留在一种情况下:当user1进入R且user2进入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 个答案:

答案 0 :(得分:0)

而不是嵌套user1和user2的测试尝试使用&amp;&amp; 你没有去找别人 即。

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

}else...

P.S。尝试总是使用带有if语句的大括号,这是一个很好的做法。(即使不是必需的)。

答案 1 :(得分:0)

  1. 尝试在if子句中使用大括号。
  2. 您可以通过否定“!”轻松检查输入错误。 (即!true=false
  3. 请尝试使用&amp;&amp ;;运营商要结合。
  4. 您还可以将输入转换为整数并对其进行切换,这是针对这种if-elseif-else语句而生成的。
  5. 这将有助于您对发生的事情保持良好的概述,并且您可以更轻松地找到错误。

    请注意,创建一个方法来确定哪个玩家拥有一个玩家可能也很聪明,因为它给两个字符串作为参数并让它返回一个int / boolean,其中哪个玩家更强大:

    /**
     * @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:请注意,这里不应该回答家庭作业。