代码喝注释如下:
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!") } }
大哥,switch case 不是这样用的,case后面a的值只能是确定的一个,比如:case 1:
System.out.println("春季")
break
case 2:
System.out.println("春季")
break
............
你要是想用范围做条件,用 if else 语句:
if(1<=a<=3){
System.out.println("春季")
}else if(4<=a<=6){
System.out.println("夏季")
}..........
else {
System.out.println("无季节匹配")
}
public static void season() {Scanner scanner = new Scanner(System.in)
System.out.println("请输入月份")
int month = scanner.nextInt()
if (month >= 1 && month <= 3) {
System.out.println("该季节为春季")
} else if (month >= 4 && month <= 6) {
System.out.println("该季节为夏季")
} else if (month >= 7 && month <= 9) {
System.out.println("该季节为秋季")
} else if (month >= 10 && month <= 12) {
System.out.println("该季节为冬季")
} else {
System.out.println("...")
}
scanner.close()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)