编写一个Java程序,接受用户输入的一个1――12之间的整数(如果输入的数据不满足这个条件,则要求用户重新输入),利用switch语句输出对应的月份的天数。
test.java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
public class test{ public static void main(String[] args) throws IOException
{ System.out.println("请输入月份:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int i=Integer.parseInt(br.readLine()); while (i>12 || i<1){ System.out.println("请重新输入!"); br = new BufferedReader(new InputStreamReader(System.in)); a=Integer.parseInt(br.readLine()); } System.in.close(); br.close();
switch (i){
case 1: System.out.println("本月共 "+31+" 天"); break;
case 2: System.out.println("本月共 "+28+"或"+29+" 天"); break;
case 3: System.out.println("本月共 "+31+" 天"); break;
case 4: System.out.println("本月共 "+30+" 天"); break;
case 5: System.out.println("本月共 "+31+" 天"); break;
case 6: System.out.println("本月共 "+30+" 天"); break;
case 7: System.out.println("本月共 "+31+" 天"); break;
case 8: System.out.println("本月共 "+31+" 天"); break;
case 9: System.out.println("本月共 "+30+" 天"); break;
case 10: System.out.println("本月共 "+31+"天"); break;
case 11: System.out.println("本月共 "+30+" 天"); break;
case 12: System.out.println("本月共 "+31+" 天"); break;
default: System.out.println("请重新输入!"); } } }
|
D:\java>javac test.java
D:\java>java test
请输入月份:
阅读(9123) | 评论(0) | 转发(0) |