如何让 monthName 显示?
How do I get the monthName to display?
这是我的代码:
public class CalendarDisplay {
public static boolean isLeapYear(int year)
{
return (year %400 == 0) ||
(year %4 == 0 && year %100 != 0);
}
public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
int month = Integer.parseInt(args [1]);
boolean leapYear = isLeapYear(year);
System.out.println("The Year " + year + ((leapYear == true)?
" is": " is not") + " a leap year");
int days = 0;
switch(month)
{
case 4: case 6: case 9: case 11:
days=30;
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
case 2:
days = (leapYear == true)? 29:28;
break;
default:
System.out.println ("error!");
break;
}
System.out.println("The Month " + month + " has " + days);
}
public static String getMonthName (int month) {
String monthName = "";
switch(month)
{
case 1:
monthName = "January";
break;
case 2:
monthName = "Feburary";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;
}
return monthName;
}
}
在命令行我输入:2014 02
输出是这样的:
"2014年不是闰年
第 2 个月有 28
我需要 "Month 2" 说 "Month February"
谢谢
由于您的 getMonthName
函数 returns 一个字符串值,您的 System.out.println
可以调用 getMonthName
方法并将结果用作输出的一部分。
System.out.println("The Month " + getMonthName(month) + " has " + days + " days");
这是我的代码:
public class CalendarDisplay {
public static boolean isLeapYear(int year)
{
return (year %400 == 0) ||
(year %4 == 0 && year %100 != 0);
}
public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
int month = Integer.parseInt(args [1]);
boolean leapYear = isLeapYear(year);
System.out.println("The Year " + year + ((leapYear == true)?
" is": " is not") + " a leap year");
int days = 0;
switch(month)
{
case 4: case 6: case 9: case 11:
days=30;
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
case 2:
days = (leapYear == true)? 29:28;
break;
default:
System.out.println ("error!");
break;
}
System.out.println("The Month " + month + " has " + days);
}
public static String getMonthName (int month) {
String monthName = "";
switch(month)
{
case 1:
monthName = "January";
break;
case 2:
monthName = "Feburary";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;
}
return monthName;
}
}
在命令行我输入:2014 02
输出是这样的:
"2014年不是闰年 第 2 个月有 28
我需要 "Month 2" 说 "Month February"
谢谢
由于您的 getMonthName
函数 returns 一个字符串值,您的 System.out.println
可以调用 getMonthName
方法并将结果用作输出的一部分。
System.out.println("The Month " + getMonthName(month) + " has " + days + " days");