正如@AlexandreSantos 指出的,每次重启游戏时都需要重新初始化maxRolls 和sum 的值。也就是说,这些初始化应该是在您的 do {} while () 循环中执行的第一件事。
do {
int maxRolls = 7;
int sum = 0;
// ...
} while (option);
我还会给你其他建议:
- 在Java 中,按照惯例,类名以大写字母开头。因此,我会将您的班级命名为
Game 而不是 game。
以下代码(以及与"no" 等效的代码):
(userInputTwo.equals("Yes") || userInputTwo.equals("yes") || userInputTwo.equals("YES"))
... 可以替换为:
userInputTwo.equalsIgnoreCase("yes")
...因为,正如您在问题中提到的那样,您实际上只是试图忽略这种情况;)
- 您正在在两个地方询问用户是否要重新启动。在打印
"You won" 或 "You lost" 之后,您实际上可以(应该)简单地执行一次。
我建议更换:
if (sum >= 43) {
System.out.println("You Win");
System.out.print("Would You Like To Play Again . Yes or No?");
final String userInput = input.nextLine();
if (userInput.equals("Yes") || userInput.equals("yes") || userInput.equals("YES")) {
// MISSING CODE TO RESTART THE PROGRAM
option = true;
} else if (userInput.equals("No") || userInput.equals("no") || userInput.equals("NO")) {
System.exit(0);
}
}
if (sum < 43 || sum % 10 == 0) {
System.out.println("You Lose");
System.out.print("Would You Like To Play Again . Yes or No?");
final String userInputTwo = input.nextLine();
if (userInputTwo.equals("Yes") || userInputTwo.equals("yes") || userInputTwo.equals("YES")) {
option = true;
// MISSING CODE TO RESTART THE PROGRAM
} else if (userInputTwo.equals("No") || userInputTwo.equals("no") || userInputTwo.equals("NO")) {
System.exit(0);
}
}
...作者:
if (sum >= 43) {
System.out.println("You Win");
}
if (sum < 43 || sum % 10 == 0) {
System.out.println("You Lose");
}
System.out.print("Would You Like To Play Again . Yes or No?");
final String userInput = input.nextLine();
if ("yes".equalsIgnoreCase(userInput) {
// MISSING CODE TO RESTART THE PROGRAM
option = true;
} else if ("no".equalsIgnoreCase(userInput)) {
System.exit(0);
}
...或者,更好的是,将其提取到其他方法中。
或者,甚至更好,甚至不检查其中一种可能性并将其设为默认值,以防用户输入既不是"yes" 也不是"no" 的内容:
private static boolean restart(final Scanner input) {
// I choose to interpret any input that's different from "yes" as a "no".
System.out.print("Would You Like To Play Again. Yes or No? (default: No)");
final String userInput = input.nextLine();
if ("yes".equalsIgnoreCase(userInput)) {
return true;
}
return false;
}
... 这显然可以变成:
private static boolean restart(final Scanner input) {
// I choose to interpret any input that's different from "yes" as a "no".
System.out.print("Would you like to play again? [Yes/No] (default: No)");
return "yes".equalsIgnoreCase(input.nextLine());
}
...option 变量可能会消失:
do {
...
} while (Game.restart(input));
- 您可以(应该)使用
Random 而不是Math.random(),这样更方便。
例如:
final int dieOne = (int) (Math.random() * faces) + 1;
final int dieTwo = (int) (Math.random() * faces) + 1;
final int totalRollForRound = dieOne + dieTwo;
...可能变成:
// Outside of the do {} while ():
final Random r = new Random();
// Inside the do {} while ():
final int totalRollForRound = r.nextInt(faces) + r.nextInt(faces) + 2;
使用try-with-resources 语法:
private static boolean restart() {
try (final Scanner input = new Scanner(System.in) {
// I choose to interpret any input that's different from "yes" as a "no".
System.out.print("Would you like to play again? [Yes/No] (default: No)");
return "yes".equalsIgnoreCase(input.nextLine());
}
}
- 最后一件事:你的
sum % 10 == 0 很奇怪:你已经告诉用户,如果他得分至少为 43,他就赢了,如果他得分低于 43,他就会输……你应该:李>
在检查用户得分是否超过 43 之前测试该条件(因此也会拒绝 50、60、70、80 等分数...)
... 或:
忘记这条规则,它只旨在拒绝 score < 43 规则已经涵盖的 10、20、30 和 40。
干杯 ;)
只是因为我觉得无聊,我实际上将我自己的建议(以及其他一些建议)应用于您的代码:
import java.util.Random;
import java.util.Scanner;
public class Game {
private static final int FACES = 6;
private static final int MAX_ROLLS = 7;
private static final Random R = new Random();
public static void main(final String[] args) {
try (final Scanner input = new Scanner(System.in)) {
do {
if (Game.roll() >= 43) {
System.out.println("You won!");
} else {
System.out.println("You lost.");
}
} while (Game.restart(input));
}
}
private static int roll() {
int maxRolls = MAX_ROLLS;
int sum = 0;
for (int i = 1; i < maxRolls; i++) {
final int dieOne = R.nextInt(FACES) + 1;
final int dieTwo = R.nextInt(FACES) + 1;
sum += dieOne + dieTwo;
System.out.println("Roll #" + i + ": You rolled " + dieOne + " and " + dieTwo + ".\tYour new total is: " + sum);
if (dieOne == dieTwo) {
System.out.println("DOUBLES! You get an extra roll.");
maxRolls++;
}
}
return sum;
}
private static boolean restart(final Scanner input) {
System.out.print("Play again? [Yes/No] (default: No): ");
return "yes".equalsIgnoreCase(input.nextLine());
}
}