java 一个简单的闰年逻辑麻烦
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7387980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A simple leap year logic trouble
提问by John Cooper
public class LeapYear {
public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
boolean isLeapYear;
// divisible by 4
isLeapYear = (year % 4 == 0);
// divisible by 4 and not 100
isLeapYear = isLeapYear && (year % 100 != 0);
// divisible by 4 and not 100 unless divisible by 400
isLeapYear = isLeapYear || (year % 400 == 0);
System.out.println(isLeapYear);
}
}
I am passing 1900
as my input. The first condition evaluates to true as its divisible by 4, but again 1900
should be divisible by 100 too...
我通过1900
作为我的输入。第一个条件评估为真,因为它可以被 4 整除,但同样也1900
应该被 100 整除......
How come i am getting 1900 as not a leap year.... what is the value being passed in the second && condition... (year % 100 !=0)
为什么我得到 1900 年不是闰年.... 在第二个 && 条件中传递的值是多少... (year % 100 !=0)
Update
更新
public class TestSample {
public static void main(String[] args){
int leapYear = Integer.parseInt(args[0]);
boolean isLeapYear;
isLeapYear = (leapYear % 4 == 0) && (leapYear % 100 != 0);
System.out.println("Its Leap Year" +isLeapYear);
}
}
Compiling this program prints 1900
not a leapYear How???? Here i am not even checking for the whether its divisible by 400 or not.
编译这个程序打印的1900
不是闰年怎么办???在这里,我什至没有检查它是否可以被 400 整除。
回答by Mike Kwan
To explain your code:
解释你的代码:
isLeapYear = (year % 4 == 0);
// isLeapYear = true
isLeapYear = isLeapYear && (year % 100 != 0);
// year % 100 IS 0. so the second part evaluates to false giving
// true && false which yields isLeapYear as false
isLeapYear = isLeapYear || (year % 400 == 0);
// this is just false || false
// which evaluates to false
Another suggestion I have for you is to just use the GregorianCalendarto find what you want:
我给你的另一个建议是使用GregorianCalendar来找到你想要的:
GregorianCalendar cal = new GregorianCalendar();
System.out.println( cal.isLeapYear(1900) );
回答by President James K. Polk
1900 is not a leap year. 1600 is a leap year, 1700, 1800, and 1900 are notleap years, then 2000 is again a leap year, etc. So your code is correct. That's good news, right?
1900年不是闰年。1600 是闰年,1700、1800 和 1900不是闰年,然后 2000 又是闰年,等等。所以你的代码是正确的。这是个好消息,对吧?
回答by Nicola Musatti
You're aware that 1900 was not a leap year, right? So the answer is correct.
你知道 1900 年不是闰年,对吧?所以答案是正确的。
// divisible by 4 and not 100
isLeapYear = isLeapYear && (year % 100 != 0);
This does exactly what is written in the comment. 1900 is divisible by 4 and100, so it doesn't match the above condition. By contrast 1904 is divisible by 4 and notby 100, so it matches.
这正是评论中所写的。1900 能被 4和100整除,所以不符合上述条件。相比之下,1904 可以被 4 整除,不能被 100整除,所以它匹配。
回答by woliveirajr
The first question is already explained, right!
第一个问题已经解释过了,对吧!
So the update:
所以更新:
isLeapYear = (leapYear % 4 == 0) && (leapYear % 100 != 0);
means
方法
isLeapYear = (1900 / 4 leaves 0 as remainder, and 0 ==0 is true)
AND
( 1900 / 100 leaves 0 as remainder, so 0 != 0 is FALSE);
isLeapYear = true AND false ==> false
and the answer is ... false. That's what you've got.
答案是......错误。这就是你所拥有的。
And 1900 wasn't a leap year, anyway.
无论如何,1900 年不是闰年。
Check at any site, like http://www.onlineconversion.com/leapyear.htmor http://www.dataip.co.uk/Reference/LeapYear.php
在任何网站上查看,例如http://www.onlineconversion.com/leapyear.htm或http://www.dataip.co.uk/Reference/LeapYear.php
回答by Ashish Maske
if ((year %4==0 && year % 100 != 0)||(year % 400 == 0))
if (((年 %4==0 && 年 % 100 != 0)||(年 % 400 == 0))
{
System.out.println(" Year is LEAP "+year);
}
else{
System.out.println(" Year is NOT LEAP "+year);
}