重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
你好,按照你的要求代码如下,给出了注释,可以直接运行
创新互联公司主营怀仁网站建设的网络公司,主营网站建设方案,手机APP定制开发,怀仁h5微信小程序搭建,怀仁网站营销推广欢迎怀仁等地区企业咨询
public class Example10_3 {
public static void main(String args[]) {
// 从1月打到12月
for (int i = 1; i = 12; i++) {
System.out.println(getSeason(i));
}
}
// 根据月份获得季节
private static String getSeason(int month) {
switch ((month - 1) / 3) {
case 0:
return "春";
case 1:
return "夏";
case 2:
return "秋";
case 3:
return "冬";
default:
return "错误";
}
}
}
话不多说,直接上代码。
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的情况,提醒输入错误 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!"); } }