java程序,求解!

来源:百度知道 编辑:UC知道 时间:2024/05/06 01:13:29
编写一个Java程序,接受用户输入的一个1――12之间的整数(如果输入的数据不满足这个条件,则要求用户重新输入),利用switch语句输出对应的月份的天数。

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;