【发布时间】:2012-09-18 08:42:11
【问题描述】:
我已经编写了一个测试闰年的代码,但希望添加一个修正来说明它是否在过去。为此,我添加了一个条件,即如果输入恰好是 2012 年或更大,那么现在时证明是正确的,否则它采用过去时。就目前而言,它忽略了这个限定符,只执行第一条语句。任何帮助完成这项工作将不胜感激。
代码如下
{
int input;
System.out.println("Enter year:");
Scanner keyboard = new Scanner(System.in);
input = keyboard.nextInt();
while (input >= 2012){
if ((input%4==0) && input%100!= 0)
{
System.out.println(input + " is a leap year.");
}
else if (input%400==0)
{
System.out.println(input + " is a leap year.");
}
else
{
System.out.println(input + " is not a leap year.");
}
}
while (input < 2012){
if ((input%4==0) && input%100!= 0)
{
System.out.println(input + " was a leap year.");
}
else if (input%400==0)
{
System.out.println(input + " was a leap year.");
}
else
{
System.out.println(input + " was not a leap year.");
}
}
}
}
【问题讨论】:
-
你的 while 循环永远不会结束
标签: java loops if-statement while-loop