如果在JAVA闰年7小时内发生声明

时间:2015-02-22 02:27:45

标签: java date if-statement monthcalendar

我有4周的Java课程,有0经验。 我要求创建一个程序,要求用来输入一个月的数字表示,例子1将是1月和1月。也是年度的输入,并且程序输出显示月份 月份中的天数如果是闰年。这是我多次缩短的尝试之一。我做错了什么,从1月开始就是输入。

package leapmonth;

import javax.swing.JOptionPane;

public class LeapMonth {
    public static void main(String[] args) {

        int Year;
        int MonthNumber;

    String MonthString;    


    JOptionPane.showMessageDialog(null, " HELLO, WELCOME TO LEAPMONTH ");
    MonthString = JOptionPane.showInputDialog( " Please enter the numerical respresentation of the month");
    MonthNumber = Integer.parseInt(MonthString);
    MonthString = JOptionPane.showInputDialog( " Please enter the year");
    Year = Integer.parseInt(MonthString);

    if( MonthNumber == 1 && Year % 4 == 0 && Year % 100 != 0 || Year % 400 == 0)
    {
     JOptionPane.showMessageDialog(null,"The month is January. " + "January has 31 days in it" + " and is also a leap year "); 
    }   
    else if( MonthNumber == 1 && Year % 4 != 0 && Year % 100 == 0 || Year % 400 != 0)
    {
     JOptionPane.showMessageDialog(null,"The month is January. " + "January has 31 days in it" + " and it is not a leap year ");   
    }
    else if( MonthNumber == 2 && Year % 4 == 0 && Year % 100 != 0 || Year % 400 ==0)
    {
     JOptionPane.showMessageDialog(null,"The month is February. " + "February has 29 days in it because this is a leap year ");  
    } 
    else if( MonthNumber == 2 && Year % 4 != 0 && Year % 100 == 0 || Year % 400 != 0)
    {
    JOptionPane.showMessageDialog(null,"The month is February. " + "February has 28 days in it because this is not a leap year ");
    }
   }
}

4 个答案:

答案 0 :(得分:2)

这并不意味着你认为这意味着:

MonthNumber == 1 && Year % 4 == 0 && Year % 100 != 0 || Year % 400 == 0

我认为你想要的是:

MonthNumber == 1 && Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)

强调括号。

说明:||&&运算符都具有相同的优先级,因此它们从左到右关联。

如果没有括号,那么你的逻辑是有缺陷的,任何400的倍数都会产生true

例如,假设Year=2000MonthNumber=25

然后

MonthNumber == 1 && Year % 4 == 0 && Year % 100 != 0 || Year % 400 == 0

评估为

false && true && false || true

一步一步然后评估为

((false && true) && false) || true
(false && false) || true
false || true
true

在该示例中,只要最后一个条件产生true,那么整个表达式也将产生true。我认为这不是你想要的。

注意:我没有准确地花时间来验证你的逻辑。我只是发现了这个简单的错误,但可能还有其他错误。

答案 1 :(得分:2)

你的代码中有很多重复。将它拆分出来,让事情变得更容易。您的许多变量(一般而言,不仅仅是编程术语)只会改变一次。年份只在月份是2月才有意义。

String MonthString;
int Days;
bool IsLeapYear = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
String Leap = "";
if (!IsLeapYear) {
    Leap = "not ";
}

if (MonthNumber == 1) {
    MonthString = "January";
    Days = 31;
}
else if (MonthNumber == 2) {
    MonthString = "February";
    if (IsLeapYear) {
        Days = 29;
    }
    else {
        Days = 28;
    }
}
else if (MonthNumber == 3) {
    MonthString = "March";
    Days = 31;
}
// etc....

JOptionPane.showMessageDialog(null, "The month is " + MonthString + ". " + 
    MonthString + " has " + Days + " days in it and it is " + Leap + 
    "a leap year.");

答案 2 :(得分:2)

iFytil's answer是正确的。 这只是boolean algebra laws的问题。我建议你阅读它并了解基础知识。 这将有助于复杂条件的许多未来实现,特别是那些具有多个否定的条件。

此外,您的代码显示了一些过于复杂的情况。我不确定你的作业中是否有任何规则,但如果你单独处理这些规则,它会更加清晰。 不是一次评估两个东西(哪个月和闰年与否),导致12 x 2条件,一个好的方法是将它分成两个,导致12 + 2条件(如果在2月内有一点) 。此外,当您进行两次以上的比较时,switch-case结构更具可读性。

让我解释一下:

String answer;
switch (MonthNumber) {
    case 1:
        answer = "The month is January. January has 31 days in it.";
        break;
    case 2:
        answer = "The month is February. ";
        if (Year % 4 == 0 && !(Year % 100 == 0 && Year % 400 != 0))
            // years divisible by 4, except those divisible by 100 and not by 400
            answer += "February has 29 days in it because this is a leap-year.";
        else
            answer += "February has 28 days in it because this is not a leap-year.";
        break;
    case 3:
        ... // you get the idea
}

然后,在它之后,你解析闰年文本。

if (MonthNumber != 2) { // you have already completed the text in Feb
    if (Year % 4 == 0 && !(Year % 100 == 0 && Year % 400 != 0))
        answer += "and is also a leap-year.";
    else
        answer += "and it is not a leap-year.";
}

输出文本中的一个小变化将无需检查它是否是二月,但这只是一个技巧。

答案 3 :(得分:1)

可以像这样简化。

package leapmonth;

import javax.swing.JOptionPane;

public class LeapMonth {
    public static void main(String[] args) {

    String MonthInput;
    int YearInput;
    String[] MonthArray = {"January","February","March"
                            ,"April","May","June"
                            ,"July","August","September"
                            ,"October","November","December"
                          }; 
    // array always started from 0 then January should be 0

    JOptionPane.showMessageDialog(null, " HELLO, WELCOME TO LEAPMONTH ");
    Input = JOptionPane.showInputDialog( "Please enter the numerical respresentation of the month : ");
    MonthInput = Integer.parseInt(Input);
    Input = JOptionPane.showInputDialog( "Please enter the year : ");
    YearInput = Integer.parseInt(Input);

    if( MonthInput != 2 && Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0))
    {
     JOptionPane.showMessageDialog(null,"The month is " + MonthArray[MonthInput+1] + ". " + MonthArray[MonthInput+1] + " has 31 days in it" + " and is also a leap year "); 
    }   
    else if( MonthInput != 2 && Year % 4 != 0 && (Year % 100 == 0 || Year % 400 != 0))
    {
     JOptionPane.showMessageDialog(null,"The month is " + MonthArray[MonthInput+1] + ". " + MonthArray[MonthInput+1] + " has 31 days in it" + " and it is not a leap year ");   
    }
    else if( MonthInput == 2 && Year % 4 == 0 && (Year % 100 != 0 || Year % 400 ==0))
    {
     JOptionPane.showMessageDialog(null,"The month is " + MonthArray[MonthInput+1] + ". " + MonthArray[MonthInput+1] + " has 29 days in it because this is a leap year ");  
    } 
    else if( MonthInput == 2 && Year % 4 != 0 && (Year % 100 == 0 || Year % 400 != 0))
    {
    JOptionPane.showMessageDialog(null,"The month is " + MonthArray[MonthInput+1] + ". " + MonthArray[MonthInput+1] + " has 28 days in it because this is not a leap year ");
    }
   }
}