日期:2014-05-20  浏览次数:21002 次

java swith小程序。。
刚写的JAVA小程序出错了
哪为高手给看看。。万分感谢

import java.io.*; 
public class MyDate
{
  public static void main(String args[])
  {
 int day=1;
 int month=1;
 int year=2008;
InputStreamReader reader = new InputStreamReader(System.in); 
BufferedReader input=new BufferedReader(reader); 
  try
{
  System.out.println("请输入年:(year>=1700 && year<=2010)"); 
  year=Integer.parseInt(input.readLine()); 
  System.out.println("你输入月:month>=1 && month<=12"); 
  month=Integer.parseInt(input.readLine()); 
  System.out.println("请输入日:day>=1 && day<=31"); 
  day=Integer.parseInt(input.readLine()); 
  if(year>=1700 && year<=2010)
  {  
if((year%4==0&&year%100!=0)||(year%400==0))
{
  System.out.println("闰年");
  swith(month)
  { 
case 1,3,5,7,8,10,12:
  if(day>=1 && day<=31)
  System.out.println("您输入的日期:"+year+"-"+month+"-"+day);
break;
  case 2:
  if(day>=1 && day<=29)
  System.out.println("您输入的日期:"+year+"-"+month+"-"+day);
break;
case 4,6,9,11:
if(day>=1 && day<=30)
  System.out.println("您输入的日期:"+year+"-"+month+"-"+day);
break;
default:
  System.out.println("信息错误");
  break;
}
  }
  else
  {
  System.out.println("非闰年");
swith(month)
  { 
case 1,3,5,7,8,10,12:
  if(day>=1 && day<=31)
  System.out.println("您输入的日期:"+year+"-"+month+"-"+day);
  break;
  case 2:
  if(day>=1 && day<=28)
  System.out.println("您输入的日期:"+year+"-"+month+"-"+day);
  break;
case 4,6,9,11:
if(day>=1 && day<=30)
  System.out.println("您输入的日期:"+year+"-"+month+"-"+day);
break;
default:
  System.out.println("信息错误");
break;
}


  }
  else {System.out.println("信息错误");}
  }

  catch(IOException e)
  { 
System.err.println("Error is "+e.getMessage());
  } 
  }
}

------解决方案--------------------
switch全都写错了,case的用法完全乱搞,改了一下
Java code

import java.io.*;

public class MyDate {

    public static void main(String args[]) {
        int day = 1;
        int month = 1;
        int year = 2008;
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader input = new BufferedReader(reader);
        try {
            System.out.println("请输入年:(year>=1700 && year <=2010)");
            year = Integer.parseInt(input.readLine());
            System.out.println("你输入月:month>=1 && month <=12");
            month = Integer.parseInt(input.readLine());
            System.out.println("请输入日:day>=1 && day <=31");
            day = Integer.parseInt(input.readLine());
            if (year >= 1700 && year <= 2010) {
                if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                    System.out.println("闰年");
                    switch (month) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        if (day >= 1 && day <= 31)
                            System.out.println("您输入的日期:" + year + "-" + month
                                    + "-" + day);
                        break;
                    case 2:
                        if (day >= 1 && day <= 29)
                            System.out.println("您输入的日期:" + year + "-" + month
                                    + "-" + day);
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        if (day >= 1 && day <= 30)
                            System.out.println("您输入的日期:" + year + "-" + month
                                    + "-" + day);
                        break;
                    default:
                        System.out.println("信息错误");
                        break;
                    }
                } else {
                    System.out.println("非闰年");
                    switch (month) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        if (day >= 1 && day <= 31)
                            System.out.println("您输入的日期:" + year + "-" + month
                                    + "-" + day);
                        break;
                    case 2:
                        if (day >= 1 && day <= 28)
                            System.out.println("您输入的日期:" + year + "-" + month
                                    + "-" + day);
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        if (day >= 1 && day <= 30)
                            System.out.println("您输入的日期:" + year + "-" + month
                                    + "-" + day);
                        break;
                    default:
                        System.out.println("信息错误");
                        break;
                    }
                }
            } else {
                System.out.println("信息错误");
            }
        } catch (IOException e) {
            System.err.println("Error is " + e.getMessage());
        }
    }
}