离沙井最近的高铁站:java程序,求解!
来源:百度文库 编辑:中科新闻网 时间:2024/05/13 15:49:22
import java.io.*;
class Months
{
public static void main(String[] args)
{
String month=null;
int n=0;
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
while(n<=0||n>=13)
{
System.out.println("请输入月份:1~12");
month=in.readLine();
try
{
n=Integer.parseInt(month);
}catch(NumberFormatException ne)
{
n=0;
}
}
}catch(IOException e)
{
e.printStackTrace();
}
switch(n)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(n+"月有"+31+"天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(n+"月有"+30+"天");
break;
case 2:
System.out.println(n+"月有"+28+"天(闰年29天)");
break;
}
}
}
先初始化
把月份和天数作为key/value对,放到map里面
然后根据key来取value就可以了
不过闰年呢?