我有这段代码:
switch(month){
case 1:
System.out.print("January");
break;
case 2:
System.out.print("February");
break;
case 3:
System.out.print("March");
break;
case 4:
System.out.print("April");
break;
case 5:
System.out.print("May");
break;
case 6:
System.out.print("June");
break;
case 7:
System.out.print("July");
break;
case 8:
System.out.print("August");
break;
case 9:
System.out.print("September");
break;
case 10:
System.out.print("October");
break;
case 11:
System.out.print("November");
break;
case 12:
System.out.print("December");
break;
}
好的,所以这段代码100%完美地工作...对于int月。我有另一个int(avgMonth)只能保持相同的值(1-12),我想只有相同的输出(月份)。如何在不必复制整个交换机和案例的情况下将avgMonth添加到此代码中? 我尝试使用逗号(月,avgMonth)和&&'s(月和& avgMonth)以及+'s(月+ avgMonth),但无济于事。
答案 0 :(得分:5)
将整个代码块封装在方法中,并将avgMonth
和month
作为参数传递。像这样:
public static void monthNumToName(int month) {
// … same as before
}
或者,您可以使用Map
:
private static final Map<Integer, String> months;
static {
months.put(1, "January");
months.put(2, "February");
months.put(3, "March");
months.put(4, "April");
months.put(5, "May");
months.put(6, "June");
months.put(7, "July");
months.put(8, "August");
months.put(9, "September");
months.put(10, "October");
months.put(11, "November");
months.put(12, "December");
}
public static void monthNumToName(int month) {
String name = months.get(month);
if (name == null)
throw new IllegalArgumentException("Invalid month number: " + month);
System.out.print(name);
}
或更简单,只需使用数组,因为我们事先知道月份数量在1-12范围内受到限制:
private static final String[] months = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
public static void monthNumToName(int month) {
if (month < 1 || month > 12)
throw new IllegalArgumentException("Invalid month number: " + month);
System.out.print(months[month-1]);
}
无论如何,当你需要打印月份名称时,请执行以下操作:
monthNumToName(month);
monthNumToName(avgMonth);
答案 1 :(得分:2)
一个简单的数组可以完成这项工作,比如
String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
那么你的月份就像
monthName = monthNames[month - 1];
avgMonthName = monthNames[avgmonth - 1];
答案 2 :(得分:2)
您可以使用DateFormatSymbols
方法{/ 3}}。
获取月份字符串。例如:“1月”,“2月”等
public static String getMonth(int month){
return new DateFormatSymbols(Locale.UK).getMonths()[month-1];
}
month-1
,因为你希望1用1月映射getMonths()
答案 3 :(得分:1)
怎么样
private static String[] MONTHS = "none,January,February,March,April,May,June,July,August,September,October,November,December".split(",");
public static void printMonth(int month) {
System.out.print(MONTHS[month]);
}
答案 4 :(得分:1)
首先,删除那个switch
并假装你从来没有写过如此混乱的东西。
现在你有一个干净的平板,创建一个enum
:
public enum Month {
JANUARY(1),
FEBRUARY(2),
//other months
;
private static final Map<Integer, Month> LOOKUP;
static {
final Map<Integer, Month> lookup = new HashMap<>();
for (final Month month : values()) {
lookup.put(month.number, month);
}
LOOKUP = Collections.unmodifiableMap(lookup);
}
public static Month fromNumber(final int num) {
final Month month = LOOKUP.get(num);
if(month == null)
throw new IllegalArgumentException("No month for number " + num);
return month;
}
private final int number;
private Month(final int number) {
this.number = number;
}
@Override
public String toString() {
final String name = name();
return name.substring(0, 1) + name.substring(1).toLowerCase();
}
}
并像这样使用
final Month month = Month.fromNumber(someInt);
为了打印月份,您可以:
System.out.println(month);
toString
方法会将其转换为句子。