你好,按照你的要求代碼如下,給出了注釋,可以直接運(yùn)行
創(chuàng)新互聯(lián)公司主營懷仁網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,手機(jī)APP定制開發(fā),懷仁h5微信小程序搭建,懷仁網(wǎng)站營銷推廣歡迎懷仁等地區(qū)企業(yè)咨詢
public class Example10_3 {
public static void main(String args[]) {
// 從1月打到12月
for (int i = 1; i = 12; i++) {
System.out.println(getSeason(i));
}
}
// 根據(jù)月份獲得季節(jié)
private static String getSeason(int month) {
switch ((month - 1) / 3) {
case 0:
return "春";
case 1:
return "夏";
case 2:
return "秋";
case 3:
return "冬";
default:
return "錯(cuò)誤";
}
}
}
話不多說,直接上代碼。
Scanner sc = new Scanner(System.in);
System.out.print("請輸入月份 : ");
String monthStr = sc.next();
int month = Integer.parseInt(monthStr);
if (month = 3 month = 5) {
System.out.println("春季");
} else if (month = 6 month = 8) {
System.out.println("夏季");
} else if (month = 9 month = 11) {
System.out.println("秋季");
} else if (month == 12 || month = 2) {
System.out.println("冬季");
} else {
System.out.println("沒有該月份 " + month);
}
代碼喝注釋如下:
public?static?void?main(String[]?args)?{??System.out.print("Please?input?the?month?to?check:");??int?month?=?new?Scanner(System.in).nextInt();//月份//月份不在1~12的情況,提醒輸入錯(cuò)誤??if?(month?=?0?||?month??12)?{???System.out.println("Error!?month?must?be?between?1?and?12!");??}?
//1-3月是春天
else?if?(month?=?3)?{???System.out.println("Month?"?+?month?+?"?is?in?Spring!");??}?
//4-6月是夏天
else?if?(month?=?6)?{???System.out.println("Month?"?+?month?+?"?is?in?Summer!");??}?
//7-9月是秋天
else?if?(month?=?9)?{???System.out.println("Month?"?+?month?+?"?is?in?Autumn!");??}?
//10-12月是冬天
else?{???System.out.println("Month?"?+?month?+?"?is?in?Winter!");??}?}