为什么我的闰年的算法不工作(Java)的?我的、闰年、算法、工作

由网友(对着背影说爱你)分享简介:下面是我:Scanner input = new Scanner(System.in);System.out.print("Enter a year: ");int Year = input.nextInt();System.out.print("Enter a month (first three letters...

下面是我:

Scanner input = new Scanner(System.in);
    System.out.print("Enter a year: ");
    int Year = input.nextInt();
    System.out.print("Enter a month (first three letters with the first"
            + " letter uppercase): ");
    String Month = input.next();

    String ThirtyOne = "Jan" + "Mar" + "May" + "Jul" + "Aug" + "Oct" + "Dec";
    String DaysThirtyOne = ThirtyOne.substring(21) + "31";

    String Thirty = "Apr" + "Jun" + "Sep" + "Nov";
    String DaysThirty = Thirty.substring(12) + "30";

    String TwentyEight = "Feb";
    String DaysTwentyEight = TwentyEight.substring(3) + "28";
    String DaysLeapYear = TwentyEight.substring(3) + "29";


    boolean isLeapYear = ((Year % 4 == 0) && (Year % 100 != 0) && (Year % 400 == 0));

    if (ThirtyOne.contains(Month)) {
        System.out.println(Month + " " + Year + " has " + DaysThirtyOne 
                + " days in it.");
    }
    if (Thirty.contains(Month)) {
        System.out.println(Month + " " + Year + " has " + DaysThirty 
                + " days in it.");
    }
    if(TwentyEight.contains(Month)) {
        System.out.println(Month + " " + Year + " has " + DaysTwentyEight 
                + " days in it.");
    }
    if (isLeapYear) {
        System.out.println(Month + " " + Year + " has " + DaysLeapYear 
                + " days in it.");
    }

我是新来编程,所以我也不会感到惊讶,如果这code看起来不成熟。不管怎么说,我有用户输入了一年一个月(前三个字母)。我创建了一个闰年,上面写着一个布尔变量的任何一年,用户需要输入4,100,和400来整除。然后,我创建了一个if语句,如果它是一个闰年打印出二月(无论一年用户输入)具有DaysLeapYear在它。我觉得我有什么毛病我的算法,因为如果我要拿出二十八的if语句,只是不停的闰年if语句,计算机甚至没有打印出来多少天二月会,如果它是一个闰年。同样,我想我会错的算法,但它可能是别的地方,我希望再看看这个,看看是否有人看到的东西我不是因为我是新来这个毕竟。

I am new to programming so I wouldn't be surprised if this code looks immature. Anyways, I have the user input a year and a month (first three letters). I created a boolean variable for a leap year that says whatever year the user inputs needs to be divisible by 4, 100, and 400. Then, I created an if statement for if it is a leap year to print out "Feb (whatever year the user inputs) has DaysLeapYear in it." I think I have something wrong with my algorithm because if I were to take out the if statement of TwentyEight and just kept the leap year if statements, the computer doesn't even print out how many days Feb would have if it was a leap year. Again, I think I'm going wrong in the algorithm, but it could be somewhere else and I was hoping for another look at this to see if someone sees something I am not since I am new to this after all.

推荐答案

首先你isLeapYear状况需要改变。

Firstly your isLeapYear condition needs to change.

boolean isLeapYear = ((Year % 4 == 0) && (Year % 100 != 0) || (Year % 400 == 0));

接下来你如果(TwentyEight.contains(月)),这需要改变考虑闰年。

Next your if(TwentyEight.contains(Month)) for this need to change to consider leap year.

if(TwentyEight.contains(Month) && !isLeapYear) {
        System.out.println(Month + " " + Year + " has " + DaysTwentyEight
                + " days in it.");
}
阅读全文

相关推荐

最新文章