首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Java闰年逻辑错误

Java闰年逻辑错误
EN

Stack Overflow用户
提问于 2015-10-26 11:57:45
回答 4查看 99关注 0票数 0

下面是我的代码,用于测试日期对于二月份是否有效。测试leapYear是否为真或假的顶部部分工作正常。如果我输入例如02/29/1963作为日期,leapYear将= false。然而,由于某些原因,代码将继续执行,就好像leapYear = true一样。最终结果将是:

代码语言:javascript
代码运行次数:0
运行
复制
else if (leapYear = true){
    if ((day > 0) && (day < 30) && (month ==2)){
        System.out.println("Your date is valid.)"`

它将输出“您的日期是有效的”。而它应该专门输出日期02/29/1963“您的日期无效,因为1963年的2月份只有28天。”);

我不明白它为什么要这样做。耽误您时间,实在对不起。已解决

代码语言:javascript
代码运行次数:0
运行
复制
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
    leapYear = true;
} else {
    leapYear = false;
}

if (leapYear = false) {
    if ((day > 0) && (day < 29) && (month == 2)) {
        System.out.println("Your date is valid.");
    } else if (day > 29) {
        System.out.println("Your date is invalid because there are only 28 days in February"
                        + "for the year " + year + ".");
    } else {
        System.out.println("Your date is invalid because the day you entered does not exist.");
    }
} else if (leapYear = true) {
    if ((day > 0) && (day < 30) && (month == 2)) {
        System.out.println("Your date is valid.");
    } else if (day > 30) {
        System.out.println("Your date is invalid because there are only 29 days in February for"
                        + "the year " + year + ".");
    } else {
        System.out.println("Your date is invalid because the day you entered does not exist.");
    }
}
EN

回答 4

Stack Overflow用户

发布于 2015-10-26 12:00:59

您的ifelse-if条件不能为assignment操作,而应为comparison

而不是if (leapYear = false){尝试

代码语言:javascript
代码运行次数:0
运行
复制
if (leapYear == false){

或者只是

代码语言:javascript
代码运行次数:0
运行
复制
if (!leapYear){

else if (leapYear = true){也是如此。它将是

代码语言:javascript
代码运行次数:0
运行
复制
else if (leapYear == true){

代码语言:javascript
代码运行次数:0
运行
复制
else if (leapYear){
票数 2
EN

Stack Overflow用户

发布于 2015-10-26 12:03:09

不能在if语句中使用赋值运算符。你必须做一个相等性检查:

代码语言:javascript
代码运行次数:0
运行
复制
 if (leapYear == false) {
        if ((day > 0) && (day < 29) && (month == 2)) {
            System.out.println("Your date is valid.");
        } else if (day > 29) {
            System.out.println("Your date is invalid because there are only 28 days in February"
                            + "for the year " + year + ".");
        } else {
            System.out.println("Your date is invalid because the day you entered does not exist.");
        }
    } else if (leapYear == true) {
        if ((day > 0) && (day < 30) && (month == 2)) {
            System.out.println("Your date is valid.");
        } else if (day > 30) {
            System.out.println("Your date is invalid because there are only 29 days in February for"
                            + "the year " + year + ".");
        } else {
            System.out.println("Your date is invalid because the day you entered does not exist.");
        }
    }
票数 1
EN

Stack Overflow用户

发布于 2015-10-26 13:53:38

我知道你不能在你的家庭作业中使用它,但仅供参考,Java在它的java.time框架中包含了这个特性。

代码语言:javascript
代码运行次数:0
运行
复制
ZonedDateTime now = ZonedDateTime.now ( ZoneId.of ( "America/Montreal" ) );
Year year = Year.from ( now );
Boolean isLeap = year.isLeap ();

转储到控制台。

代码语言:javascript
代码运行次数:0
运行
复制
System.out.println ( "year: " + year + " isLeap: " + isLeap );

运行时。

年份: 2015 isLeap: false

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33338247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档