gpt4 book ai didi

java - "Why does my lottery game ignores zeros?"

转载 作者:行者123 更新时间:2023-12-02 09:23:57 25 4
gpt4 key购买 nike

我正在做 Walter Savitch 所著的《Java 简介》一书第 4 章中的练习 3。指示:为猜测 secret 五位数代码的简单游戏开发一种算法。当用户输入对代码的猜测时,程序会返回两个值:猜测中正确位置的数字位数以及这些数字的总和。例如,如果密码为 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 的输出是“总理号码:不正确。得分:0Deuxième numéro est:正确。得分:1Troisième numéro est:正确。得分:2Quatrième numéro est:正确。得分:3Cinquième numéro est:正确。得分:4您有 4 个数字。 Leur 总高度 7"

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

最佳答案

而不是使用

String stringGuess = String.valueOf(guess);

你应该使用

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

始终将您读取的数字转换为五位数长的字符串。在当前的解决方案中,您应该会看到,如果打印 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);
}

关于java - "Why does my lottery game ignores zeros?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58477216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com