在過去的幾周裏,我一直在爲我的一個類使用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);
}
}
請閱讀[問]併發布[mcve]。 – Tunaki