从键盘输入一位整数,代表月份,编程判断指定月份属于一年中的哪个季度。如果是 12月、1 月、2 月,就属于冬季;如果是 3 月、4 月、5 月,就属于春季;如果是 6 月、7 月、8 月,就属于夏季;如果是 9 月、10 月、11 月,就属于秋季。输入其他数字时提示用户输入不正确。
代码已更新下一个版本。
package com.liu.fileupload;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by liumao 2021/2/19
**/
public class test {
public static void main(String[] args) throws IOException {
String month;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入月份:");
month = br.readLine();
String season;
if (month.equals("12") || month.equals("1") || month.equals("2")) {
season = "冬天";
} else if (month.equals("3") || month.equals("4") || month.equals("5")) {
season = "春天";
} else if (month.equals("6") || month.equals("7") || month.equals("8")) {
season = "夏天";
} else if (month.equals("9") || month.equals("10") || month.equals("11")) {
season = "秋天";
} else {
season = "不存在的月份";
}
System.out.println("该" + month + "月份是" + season);
}
}
package Exam01;
import java.util.Scanner;
//17年版本(有错误)
public class Topic05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int month = 0;
Scanner input = new Scanner(System.in);
System.out.println("请输入月份:");
int mouth=input.nextInt();
switch(month){
case 12:
case 1:
case 2:
System.out.println(mouth+"是冬季");
break;
case 3:
case 4:
case 5:
System.out.println(mouth+"是春季");
break;
case 6:
case 7:
case 8:
System.out.println(mouth+"是夏季");
break;
case 9:
case 10:
case 11:
System.out.println(mouth+"是秋季");
break;
default:
System.out.println("输入不正确");
}
}
}