gpt4 book ai didi

java - 我的猜谜游戏计数器不断上升

转载 作者:行者123 更新时间:2023-12-02 13:41:18 25 4
gpt4 key购买 nike

所以在类里面我应该做一个猜谜游戏,当你接近答案时为你提供线索。我的问题是,当我运行它并且得到一个正确的数字时,我显然会保留该数字并继续使用其他 4 个数字,当我这样做时,问题是我的正确数字计数器不断上升,即使我没有得到其他数字正确..我该如何解决这个问题?我是否可以在每个 if 语句中添加中断,或者是否可以完全退出 do while 循环?

public class GuessingGame {

public static void main(String[] args) {
int guess,numDigitsCorrect=0,sumDigitsCorrect=0,attempts=0,answer;
Random rng = new Random();
Scanner consoleScanner = new Scanner(System.in);
answer = rng.nextInt(90000) + 10000;
System.out.println("I have randomly chosen a 5-digit code for you to guess.Each time you guess,\n"
+ "I will tell you how many digits are correct and the sum of the digits that are correct."
+ "For example, if the number is \"68420\" and you guess 12468, I will respond:\n"
+ "Number of Digits Correct: 1\n"
+ "Sum of Digits Correct: 4\n"
+ "From deduction, you will know the 4 was correct in the guess."
+ "\nNow its your turn..................................................................");
do{
System.out.print("Please enter a 5-digit code (your guess): ");
guess = consoleScanner.nextInt();
int g1 = guess/10000;
int g2 = guess%10000/1000;
int g3 = guess % 10000 % 1000 / 100;
int g4 = guess % 10000 % 100 /10;
int g5 = guess % 10000 % 10 / 1;
int a1 = answer/10000;
int a2 = answer%10000/1000;
int a3 = answer % 10000 % 1000 / 100;
int a4 = answer % 10000 / 100 / 10;
int a5 = answer % 10000 % 10 / 10;
if(g1 == a1)
{
numDigitsCorrect ++;
sumDigitsCorrect += a1;
System.out.println("\nNumber of digits correct: " + numDigitsCorrect) ;
System.out.println("Sum of digits correct: " + sumDigitsCorrect);
System.out.println();
}
if(g2 == a2)
{
numDigitsCorrect ++;
sumDigitsCorrect += a2;
System.out.println("Number of digits correct: " + numDigitsCorrect) ;
System.out.println("Sum of digits correct: " + sumDigitsCorrect);
System.out.println();
}
if (g3 == a3)
{
numDigitsCorrect ++;
sumDigitsCorrect += a3;
System.out.println("Number of digits correct: " + numDigitsCorrect) ;
System.out.println("Sum of digits correct: " + sumDigitsCorrect);
System.out.println();
}
if (g4 == a4)
{
numDigitsCorrect ++;
sumDigitsCorrect += a4;
System.out.println("Number of digits correct: " + numDigitsCorrect) ;
System.out.println("Sum of digits correct: " + sumDigitsCorrect);
System.out.println();
}
if (g5 == a5)
{
numDigitsCorrect ++;
sumDigitsCorrect += a5;
System.out.println("Number of digits correct: " + numDigitsCorrect) ;
System.out.println("Sum of digits correct: " + sumDigitsCorrect);
System.out.println();
}
if(guess == answer)
{
System.out.println("****HOORAY! You solved it. You are so smart****");
break;
}
}while (guess != answer);

}

}

最佳答案

有一些问题需要修复 -

  1. 确保您的 a4a5 正确

    int a4 = answer % 10000 % 100 / 10; // note the modulus

    int a5 = answer % 10000 % 10; // note divided by 1 or remove the redundant statement
  2. 将 print 语句从 if block 移至 do block 内所有 if 的末尾,如下所示 -

    if (g1 == a1) {
    numDigitsCorrect++;
    sumDigitsCorrect += a1;
    }
    ... //other if statements
    if (guess == answer) {
    System.out.println("****HOORAY! You solved it. You are so smart****");
    break;
    }
    System.out.println("Number of digits correct: " + numDigitsCorrect);
    System.out.println("Sum of digits correct: " + sumDigitsCorrect);
  3. 而且由于您已经进行了检查

    if (guess == answer) {
    System.out.println("****HOORAY! You solved it. You are so smart****");
    break;
    }

    在您的 do 中,您可以将 while 条件更改为 true 作为 -

    do {
    ... your existing code
    } while(true);
  4. 回答

Would i be able to add breaks in each of the if statements

如果你这样做,即使是一个数字匹配,你的循环也会退出(中断)。

  • 重要的是要修复计数器,请将 do block 内的计数器初始化为

    do {
    numDigitsCorrect = 0;
    sumDigitsCorrect = 0;
    .. // existing logic
    }
  • 关于java - 我的猜谜游戏计数器不断上升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42731341/

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