【发布时间】:2015-10-04 23:07:17
【问题描述】:
我在执行这个 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!");
}
}
【问题讨论】:
-
你关闭过外部
{吗? -
从来没有听说过 do 循环,只有 do while 循环。时间在哪里?
-
"The system will print out the line and stop"那么它打印的是什么,您的预期输出是什么? -
... 看到@BalwinderSingh 的回答后,我发现
nextLine不是问题所在。 (但在你解决了 while/if 问题之后,这将是你的下一个问题)