"Why does my lottery game ignores zeros?"



我正在做 Walter Savitch 的《Java 导论》一书第 4 章中的练习 3。 指示: 开发一种算法,用于猜测秘密五位数代码的简单游戏。当用户在代码中输入猜测时,程序返回两个值:猜测中处于正确位置的位数和这些数字的总和。例如,如果密码为 os 53840,并且用户猜测 83241,则数字 3 和 4 位于正确的位置。因此,程序应响应 2 和 7。允许用户猜测固定次数

当前存在的问题:

  • 我的代码看起来很脏而且太长。我是初学者,但正在寻找提高效率的方法
  • 输入以 0 以外的任何其他数字开头的"猜测数字"时,它可以工作,但当它以 0 开头时,我的程序会忽略第一个数字

我花了两个小时。在本章中还有另外 25 个练习等着我,所以我希望其他练习更有效率。

public static void main(String[] args) {
int SecretCode = 12140;
int Win = 0;
//just checking the incrementor
int sum = 0;
System.out.println("Ceci est un jeu de lotterie. nPouvez-vous deviner le nombre à 5 chiffres caché ?n");
Scanner fromPlayer = new Scanner(System.in);
do {
System.out.println("Votre nombre porte-bonheur: ");
int guess = fromPlayer.nextInt();
//Interprets Guess int to string
String stringGuess = String.valueOf(guess);
int length = stringGuess.length();
boolean CorrectLength = (length == 5);
if (CorrectLength) {
String Firstdigit = stringGuess.substring(0, 1);
String Seconddigit = stringGuess.substring(1, 2);
String Thirddigit = stringGuess.substring(2, 3);
String Fourthdigit = stringGuess.substring(3, 4);
String Fifthdigit = stringGuess.substring(4);
//Interprets Secret Code int to string
String stringSecretCode = String.valueOf(SecretCode);
String FirstdigitCode = stringSecretCode.substring(0, 1);
String SeconddigitCode = stringSecretCode.substring(1, 2);
String ThirddigitCode = stringSecretCode.substring(2, 3);
String FourthdigitCode = stringSecretCode.substring(3, 4);
String FifthdigitCode = stringSecretCode.substring(4);
//Just checking the values that the program will compare to secret code
System.out.println("Vous avez entré n" + Firstdigit + "n" + Seconddigit + "n" + Thirddigit + "n" + Fourthdigit + "n" + Fifthdigit);
if (Firstdigit.equals(FirstdigitCode)) {
Win = Win + 1;
sum = sum + Integer.parseInt(Firstdigit);
System.out.println("Premier numéro est : correct. Score: " + Win);
} else {
System.out.println("Premier numéro est : incorrect. Score:" + Win);
}
if (Seconddigit.equals(SeconddigitCode)) {
Win = Win + 1;
sum = sum + Integer.parseInt(Seconddigit);
System.out.println("Deuxième numéro est : correct. Score: " + Win);
} else {
System.out.println("Deuxième numéro est : incorrect. Score: " + Win);
}
if (Thirddigit.equals(ThirddigitCode)) {
Win = Win + 1;
sum = sum + Integer.parseInt(Thirddigit);
System.out.println("Troisième numéro est : correct. Score: " + Win);
} else {
System.out.println("Troisième numéro est : incorrect. Score: " + Win);
}
if (Fourthdigit.equals(FourthdigitCode)) {
Win = Win + 1;
sum = sum + Integer.parseInt(Fourthdigit);
System.out.println("Quatrième numéro est : correct. Score: " + Win);
} else {
System.out.println("Quatrième numéro est : incorrect.  Score: " + Win);
}
if (Fifthdigit.equals(FifthdigitCode)) {
Win = Win + 1;
sum = sum + Integer.parseInt(Fifthdigit);
System.out.println("Cinquième numéro est : correct. Score: " + Win);
} else {
System.out.println("Cinquième numéro est : incorrect.  Score: " + Win);
}
System.out.println("Vous avez deviné " + Win + " numéros. Leur total vaut " + sum);
} else {
System.out.println("ERREUR. Il faut entrer 5 chiffres.");
}

我预计 02140 的输出为 "总理数字:不正确。成绩: 0 Deuxième numéro est : correct.成绩: 1 Troisième numéro est : correct.成绩: 2 Quatrième numéro est : correct.成绩: 3 Cinquième numéro est : correct.成绩: 4 Vous avez deviné 4 numéros.卢尔总值 7">

但实际输出是:ERREUR。Il faut entrer 5 chiffres. 好像程序没有将 0 标识为数字。

而不是使用

String stringGuess = String.valueOf(guess);

你应该使用

String stringGuess = String.format("%05d", guess);

始终将您读取的数字转换为五位数长的String。在当前解决方案中,您应该看到,如果您打印stringGuess前导零将从输入中删除。

相反,您可以使用以下代码,该代码使用Scanner类的nextLine()方法来解决问题:

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char[] expected = "53849".toCharArray();
// enter CTRL-D to stop
while (scanner.hasNext()) {
String input = scanner.nextLine();
if (input.length() != 5) {
System.err.println("wrong length");
continue;
}
char[] actual = input.toCharArray();
int digitSum = 0;
int correctDigits = 0;
for (int i = 0; i < expected.length; i++) {
if (actual[i] == expected[i]) {
correctDigits++;
digitSum += actual[i] - '0';
}
}
String msg = String.format(
"Number of correct digits: %d, their sum: %d",
correctDigits, digitSum
);
System.out.println(msg);
}

试试下面的一个,看看它是否适合你。将输入直接放入 stringGuess 中,而不是将其转换为 int 然后将其转换为 String。

int guess = fromPlayer.nextInt(); //remove this and take input into the below variable directly
String stringGuess = fromPlayer.next();

检查一下这个作为参考,为什么 int 不能存储前导零。

最新更新