我用java写了一个骰子游戏,可以掷两个骰子并保持分数。整场比赛共有四节课,但我面临的问题是我的EyesHaveIt课。它把每一轮都加在总分上,但它没有正确计算数学。我试着找出原因,但没有成功。有人能帮我找出问题出在哪里吗?
public class EyesHaveIt {
Scanner kb = new Scanner(System.in);
private int turnScore;
private int computerTotalScore;
private int playerTotalScore;
private int computerTurnNumber;
private int playerTurnNumber;
public int roundPoints;
private String userName;
//Accepts a name value from PlayGame class.
public void init(String name) {
userName = name;
}
//This method starts the game with a computer turn.
public void playGame() {
computerTurn();
}
//Computers turn to roll the dice.
public void computerTurn() {
turnScore = 0;
System.out.println("Computer's turn: ");
while (turnScore < 20) {
rollTheDice();
computerTurnNumber++;
setComputerTotalScore(turnScore);
}
getGameScore();
enterToContinue();
}
//Checks users input(enter) to continue player turn.
public void enterToContinue() {
System.out.print("nPress ENTER to continue ...");
try {
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
playerTurn();
}
//Players turn to roll the dice.
public void playerTurn() {
turnScore = 0;
System.out.println(userName + "'s turn:");
for (int i = 0; i < 5; i++) {
rollTheDice();
playerTurnNumber++;
getPlayerTotalScore(turnScore);
System.out.println("Roll again? (y/n) ");
char continueGame = kb.next().charAt(0);
continueGame = Character.toUpperCase(continueGame);
if (continueGame == 'Y') {
int test = 0;
} else {
getGameScore();
}
}
getGameScore();
computerTurn();
}
//Creates two dice from PairOfDice class and prints values rolled.
public void rollTheDice() {
PairOfDice dieOne = new PairOfDice();
PairOfDice dieTwo = new PairOfDice();
int die1 = dieOne.getDieOneValue();
int die2 = dieTwo.getDieOneValue();
System.out.println("tRolled: " + die1 + " and " + die2);
whatWasRolled(die1, die2);
}
//Accepts int from rollTheDie and checks the value.
public void whatWasRolled(int die1, int die2) {
if (die1 == 1 && die2 == 1) {
System.out.println("t Rolled snake eyes! All turn points will be doubled.");
roundPoints = (die1 + die2 * 2);
setScore(roundPoints);
} else if (die1 == 6 && die2 == 6) {
System.out.println("t Rolled box cars! All points are gone now!");
roundPoints = 0;
setScore(roundPoints);
} else if (die1 == die2) {
System.out.println("t Rolled double. . . lose all turn points.");
roundPoints = 0;
setScore(roundPoints);
} else {
roundPoints = die1 + die2;
setScore(roundPoints);
getTurnScore();
}
}
//Sets turnScore from whatWasRolled.
public void setScore(int roundPoints) {
turnScore = turnScore + roundPoints;
}
//Sets computer game score.
public void setComputerTotalScore(int turnScore) {
computerTotalScore = turnScore + computerTotalScore;
}
//Sets player game score.
public void setPlayerTotalScore(int turnScore) {
playerTotalScore = turnScore + playerTotalScore;
}
//computerTotalScore accesor returns an int.
public int getComputerTotalScore() {
return computerTotalScore;
}
//playerTotalScore accesor returns an int.
public int getPlayerTotalScore(int turnScore) {
playerTotalScore = turnScore + playerTotalScore;
return playerTotalScore;
}
//Returns turnScore after roll.
public int getTurnScore() {
System.out.println("tCurrent score for this turn:" + turnScore);
return turnScore;
}
//How the game ends and current game score is displayed.
public void getGameScore() {
System.out.println(
"CURRENT GAME SCORE: Computer: " + computerTotalScore + "t" + userName + ": " + playerTotalScore);
if (computerTotalScore >= 150) {
System.out.println("Sorry, " + userName + " you got beat by the computer!");
System.exit(0);
} else if (playerTotalScore > 150) {
System.out.println(userName + ", Congratulations! You beat the computer!");
System.exit(0);
}
}
}
你好吗?
我在两节课上改了三节课。看看它是否适合你。
//Computers turn to roll the dice.
public void computerTurn() {
computerTurnNumber = 0; // here
turnScore = 0;
System.out.println("Computer's turn: ");
while (computerTurnNumber < 20) { // here is the amount of time the dice will be rolled
rollTheDice();
computerTurnNumber++;
setComputerTotalScore(turnScore);
}
getGameScore();
enterToContinue();
}
和
//Sets turnScore from whatWasRolled.
public void setScore(int roundPoints) {
turnScore = roundPoints; //here, before it was accumulating the points of each turn
}