Java实现:输入一个月份,用if……else判断其属于什么季节。
结果如下:
代码如下:
/**
* 键盘录入月份,输出对应的季节(if...else或switch实现)
* 需求:键盘录入一个月份,输出该月份对应的季节
* 一年有四季:3,4,5春季 6,7,8夏季 9,10,11秋季 12,1,2冬季
*/
package com.jasmyn.demo;
import java.util.Scanner;
/**
* @author jasmyn
* @date 2020年11月10日下午1:57:47
* @package com.jasmyn.demo
*/
public class demo1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个月份(1-12):");
int month=sc.nextInt();
if(month==3||month==4||month==5) {
System.out.println("春季");
}else if(month==6||month==7||month==8) {
System.out.println("夏季");
}else if(month==9||month==10||month==11) {
System.out.println("秋季");
}else if(month==12||month==1||month==2) {
System.out.println("冬季");
}else {
System.out.println("输入的月份有误");
}
}
}
Java实现:输入一个月份,用switch判断其属于什么季节。
结果如下:
代码如下:
/**
* 键盘录入月份,输出对应的季节(if...else或switch实现)
* 需求:键盘录入一个月份,输出该月份对应的季节
* 一年有四季:3,4,5春季 6,7,8夏季 9,10,11秋季 12,1,2冬季
*/
package com.jasmyn.demo;
import java.util.Scanner;
/**
* @author jasmyn
* @date 2020年11月10日下午1:57:47
* @package com.jasmyn.demo
*/
public class demo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个月份(1-12):");
int month=sc.nextInt();
switch(month) {
case 1:
case 2:
case 12:
System.out.println("冬季");
break;
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
}
}
}