Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2051977
  • 博文数量: 213
  • 博客积分: 10474
  • 博客等级: 上将
  • 技术积分: 2149
  • 用 户 组: 普通用户
  • 注册时间: 2005-05-05 16:53
个人简介

顺着天性做事,逆着个性做人.

文章分类

全部博文(213)

文章存档

2020年(3)

2015年(1)

2014年(1)

2013年(1)

2012年(4)

2011年(8)

2010年(36)

2009年(17)

2008年(38)

2007年(25)

2006年(37)

2005年(42)

分类: Java

2010-05-05 23:51:37

编写一个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) |
给主人留下些什么吧!~~