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

时间:2017-03-11 04:23:50

标签: java

所以对于我来说,我应该制作一个猜谜游戏,在你接近答案的时候给你提供线索。我的问题是,当我运行它并且我得到一个数字正确时,我显然会保留该数字并继续使用其他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 个答案:

答案 0 :(得分:1)

很难解决的问题 -

  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. 如果在if区域内,则将您的打印语句从do块移出到所有区域的末尾 -

    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中,您可以将您的状态更改为true -

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

  5.   

    我是否可以在每个if语句中添加中断

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

    1. 重要的是要修复计数器,将do块中的计数器初始化为

      do {
          numDigitsCorrect = 0;
          sumDigitsCorrect = 0;
      .. // existing logic
      }