java 编写一个程序,提示用户输入年和月(前 3 个字母,第一个字母大写)然后显示该月的天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28551502/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:48:57  来源:igfitidea点击:

Write a program that prompts the user to enter a year and a month (first 3 letters, first letter uppercase) then displays the days of the month

javanetbeans

提问by Chris Redfieldea

Write a program that prompts the user to enter a year and the first three letters of a month name (with the first letter in uppercase) and displays the number of days in the month. Here is a sample run:

编写一个程序,提示用户输入年份和月份名称的前三个字母(首字母大写)并显示该月的天数。这是一个示例运行:

Enter a year: 2001

输入年份:2001

Enter a month: Jan

输入月份:Jan

"Jan 2001 has 31 days"

“2001 年 1 月有 31 天”

Enter a year: 2016

输入年份:2016

Enter a month: Feb

输入月份:二月

"Jan 2016 has 29 days" (I do not understand this one specifically) My issue is with the Leap Year portion. I don't understand how to work it in with the rest of the program because the number of days are supposed to change. Nor do I understand why the month of "Feb" changed to "Jan" in the example. I need help reworking my program. All I can use is what is seen in the code below: """If statements and switch/case."""

“2016 年 1 月有 29 天”(我不具体理解这一天)我的问题是闰年部分。我不明白如何与程序的其余部分一起工作,因为天数应该改变。我也不明白为什么在这个例子中“二月”变成了“一月”。我需要帮助修改我的程序。我所能使用的只是下面代码中的内容:"""If 语句和 switch/case。"""

import java.util.Scanner;

public class NewClass {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter a year: ");
    int year = input.nextInt();
    input.nextLine();

    System.out.print("Enter a month: ");
    String month = input.nextLine();
// Taken from the book per request of the instructor
    boolean isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));

        switch (month){
        case "Jan":
        case "Mar":
        case "May":
        case "July":
        case "Aug":
        case "Oct":
        case "Dec":
            System.out.println(month + " " + year + " has 31 days"); break;

        case "Apr":
        case "Jun":
        case "Sep":
        case "Nov":
            System.out.println(month + " " + year + " has 30 days"); break;

        case "Feb":
            System.out.println(month + " " + year + " has 28 days");
        }     
    }
 }

I'd appreciate some help or any form of offering at this point. My instructor is away for the week which leaves me in complete darkness, without proper guidance.

在这一点上,我很感激一些帮助或任何形式的提供。我的导师离开了一周,这让我完全处于黑暗之中,没有适当的指导。

采纳答案by Saul Hamadani

I ran your program. I don't exactly know why you were getting that error about Feb 2016 resulting in Jan 2016 results. When I ran it, it ran exactly how it should. However, you did not return the correct amount of days in February when a leap year occurs. You wrote out the correct equation to determine a leap year, but you didn't use it.

我运行了你的程序。我不完全知道为什么您会在 2016 年 2 月出现该错误,从而导致 2016 年 1 月的结果。当我运行它时,它完全按照它应该的方式运行。但是,当闰年发生时,您没有返回 2 月份的正确天数。你写出了确定闰年的正确方程,但你没有使用它。

    case "Sep":
    case "Nov":
        System.out.println(month + " " + year + " has 30 days"); break;

    case "Feb":
    if(isLeapYear)
    {
        System.out.println(month + " " + year + " has 29 days");
    }
        else
    {
            System.out.println(month + " " + year + " has 28 days");}
    }