编写一个程序,提示用户输入年份和月份(前 3 个字母,首字母大写),然后显示该月的日期
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
编写一个程序,提示用户输入年份和
月份名称的前三个字母(首字母大写)并显示
该月的天数。这是一个示例 运行:
输入年份:2001
输入月份:一月
"Jan 2001 has 31 days"
输入年份:2016
输入月份:二月
"Jan 2016 has 29 days"(这个我不太懂)我的问题是闰年部分。 我不明白如何将它与程序的其余部分一起使用,因为天数应该会改变。我也不明白为什么 "Feb" 的月份在示例中更改为 "Jan" 。我需要帮助修改我的程序。我只能使用下面代码中看到的内容:“”"If statements and 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");
}
}
}
在这一点上,如果能提供一些帮助或任何形式的帮助,我将不胜感激。我的导师一周不在,这让我完全处于黑暗之中,没有适当的指导。
我运行你的程序。我不完全知道为什么您会在 2016 年 2 月收到导致 2016 年 1 月结果的错误。当我 运行 它时,它 运行 它应该如何。但是,您没有 return 闰年二月的正确天数。你写出了正确的公式来确定闰年,但你没有使用它。
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");}
}
编写一个程序,提示用户输入年份和 月份名称的前三个字母(首字母大写)并显示 该月的天数。这是一个示例 运行:
输入年份:2001
输入月份:一月
"Jan 2001 has 31 days"
输入年份:2016
输入月份:二月
"Jan 2016 has 29 days"(这个我不太懂)我的问题是闰年部分。 我不明白如何将它与程序的其余部分一起使用,因为天数应该会改变。我也不明白为什么 "Feb" 的月份在示例中更改为 "Jan" 。我需要帮助修改我的程序。我只能使用下面代码中看到的内容:“”"If statements and 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");
}
}
}
在这一点上,如果能提供一些帮助或任何形式的帮助,我将不胜感激。我的导师一周不在,这让我完全处于黑暗之中,没有适当的指导。
我运行你的程序。我不完全知道为什么您会在 2016 年 2 月收到导致 2016 年 1 月结果的错误。当我 运行 它时,它 运行 它应该如何。但是,您没有 return 闰年二月的正确天数。你写出了正确的公式来确定闰年,但你没有使用它。
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");}
}