java程序 输入年月日算星期几

import javax.swing.JOptionPane;
class renyixingqiji{
public static void main(String args[])throws java.io.IOException{
int d,n,y,t,x,e,a;

n=Integer.parseInt(JOptionPane.showInputDialog("请输入年号"));
y=Integer.parseInt(JOptionPane.showInputDialog("请输入月份"));
t=Integer.parseInt(JOptionPane.showInputDialog("请输入几号"));
d=n+(n-1)/4-(n-1)/100+(n-1)/400;

if(n%4==0)
a=28;

if(!(n%4==0))
a=29;

switch(y){
case'1':x=t-1;break;
case'2':x=t+30;break;
case'3':x=a+t+30;break;
case'4':x=a+t+31+30;break;
case'5':x=a+t+31+30+30;break;
case'6':x=a+t+31+30+31+30;break;
case'7':x=a+t+31+30+31+30+30;break;
case'8':x=a+t+31+30+31+30+30+31;break;
case'9':x=a+t+31+30+31+30+30+31+31;break;
case'10':x=a+t+31+30+31+30+30+31+31+30;break;
case'11':x=a+t+31+30+31+30+30+31+31+30+31;break;
case'12':x=a+t+31+30+31+30+30+31+31+30+30+31;
}

e=(d+x)%7;

if(e==0)
JOptionPane.showMessageDialog(null,"这天是星期天");
if(!(e==0))
JOptionPane.showMessageDialog(null,"这天是星期"+e);

}

}

用JCREATER运行,显示case哪里有问题,我刚接触java两天!高手进!!小弟跪谢啦!
不行啦!你编译运行一下拉!

第1个回答  推荐于2016-03-03
没必要那么麻烦,即使不用Calendar也可以用Date啊,虽然已经不被鼓励使用了
Date date = new Date() ;
date.setYear(n) ;
date.setMonth(y+1) ;
date.setDate(t) ;
System.out.println(date.getDay()+1) ;
e = date.getDay()+1 ;
这样直接就获取是星期几了,e就是星期几
如果你实在是想练习一下switch的话,main函数我改了,以下:
public static void main( String[] args )
{
int d , n , y , t , x , e , a ;
n = Integer.parseInt(JOptionPane.showInputDialog("请输入年号")) ;
y = Integer.parseInt(JOptionPane.showInputDialog("请输入月份")) ;
t = Integer.parseInt(JOptionPane.showInputDialog("请输入几号")) ;
d = n + (n - 1) / 4 - (n - 1) / 100 + (n - 1) / 400 ;
if ( n % 4 == 0 )
a = 28 ;
else
a = 29 ;
switch (y)
{
case 1 :
x = t - 1 ;
break ;
case 2 :
x = t + 30 ;
break ;
case 3 :
x = a + t + 30 ;
break ;
case 4 :
x = a + t + 31 + 30 ;
break ;
case 5 :
x = a + t + 31 + 30 + 30 ;
break ;
case 6 :
x = a + t + 31 + 30 + 31 + 30 ;
break ;
case 7 :
x = a + t + 31 + 30 + 31 + 30 + 30 ;
break ;
case 8 :
x = a + t + 31 + 30 + 31 + 30 + 30 + 31 ;
break ;
case 9 :
x = a + t + 31 + 30 + 31 + 30 + 30 + 31 + 31 ;
break ;
case 10 :
x = a + t + 31 + 30 + 31 + 30 + 30 + 31 + 31 + 30 ;
break ;
case 11 :
x = a + t + 31 + 30 + 31 + 30 + 30 + 31 + 31 + 30 + 31 ;
break ;
case 12 :
x = a + t + 31 + 30 + 31 + 30 + 30 + 31 + 31 + 30 + 30 + 31 ;
break ;
default :
x = 0 ;
break ;
}
e = (d + x) % 7 ;
if ( e == 0 )
JOptionPane.showMessageDialog(null, "这天是星期天") ;
if ( !(e == 0) )
JOptionPane.showMessageDialog(null, "这天是星期" + e) ;
}本回答被提问者采纳
第2个回答  2008-10-09
最后一个CASE后要加个break呀!
相似回答