Java while 循环不循环

Java do while loop not looping

我在使用这个 do while 循环时遇到了一些麻烦。系统将打印出该行并停止。我究竟做错了什么?感谢您的宝贵时间!

Scanner keyboard = new Scanner(System.in);

    String answer;
    int inputNum = 1;
    int countOdd = 0;
    int countEven = 0;

    do{
        do{
            System.out.println("Please enter an interger. When you are finished, enter 0.");
            inputNum = keyboard.nextInt();

                while (inputNum % 2 == 0)
                    countOdd++;
                while (inputNum % 2 != 0)
                    countEven++;
        }while(inputNum != 0);


        System.out.println("Thank you. The amount of odd intergers you entered is " 
        + countOdd + ". The amount of even intergers you entered is " + countEven);


        System.out.println("Thank you. The amount of odd intergers you entered is " 
        + countOdd + ". The amount of even intergers you entered is " + countEven);

        System.out.println("Would you like to run this program again? (Y) (N)");
        answer = keyboard.nextLine();

    }while (answer.equals("Y"));

    System.out.println("Thank you!");
}

}

以下循环无法完成,因为 inputNum 内部未更改:

          while (inputNum % 2 == 0)
                countOdd++;
          while (inputNum % 2 != 0)
                countEven++;

此处需要 if 语句而不是 while

问题出在以下代码行

while (inputNum % 2 == 0)
                countOdd++;
while (inputNum % 2 != 0)
                countEven++;

您不对 if 条件使用 while 语句。而是使用这个

 if (inputNum % 2 == 0)
                    countOdd++;
 if (inputNum % 2 != 0)
                    countEven++;

您需要将 while 更改为 if。逻辑 inputNum%2==0 检查甚至不检查 odd.But 如果你故意相反,它很好。我按原样更改了它。

        Scanner keyboard = new Scanner(System.in);

    String answer;
    int inputNum = 1;
    int countOdd = 0;
    int countEven = 0;


        do{
            System.out.println("Please enter an interger. When you are finished, enter 0.");
            inputNum = keyboard.nextInt();

             if (inputNum % 2 == 0)
                 countEven++;
               if (inputNum % 2 != 0)
                   countOdd++;
        }while(inputNum != 0);


        System.out.println("Thank you. The amount of odd intergers you entered is " 
        + countOdd + ". The amount of even intergers you entered is " + countEven);