This article mainly introduces Java to determine the season of the month input by the user in detail. It has a certain reference value. Interested friends can refer to it
Requirements:
* Determine the season of the month based on the month entered by the user
Implementation code:
import java.util.Scanner; /** * 要求: * 根据用户输入的月份来判断该月季节 * @author Administration * */ public class JudgeSeason { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入一个月份:"); int month = input.nextInt(); //使用switch语句进行判断并做出相应的回应 //对于多个常量结果执行相同结果的业务,可在最后一个case后加break即可 switch(month){ case 12: case 1: case 2: 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; default: System.out.println("您输入的数据有错!"); } } }
Running result:
Please enter a month:
5
The month you entered belongs to spring!
Please enter a month:
9
The month you entered belongs to autumn!
The above is the detailed content of Detailed explanation of how Java determines the season of the month entered by the user. For more information, please follow other related articles on the PHP Chinese website!