这是一个作业,我是初学者。编写过程中,关于闰年判断还好办,可是判断星期几就难了,我在百度知道上找到一个什么蔡勒公式,完成后每年的1.2.10.11.12都能正常显示,其他都有错误。程序如下,有兴趣和能力的大侠给看看
import javax.swing.*;
class work2
{
public static void main(String args[])
{
int y,m;
String year,month;
year=JOptionPane.showInputDialog( "请输入年份:" );
month=JOptionPane.showInputDialog("请输入月份:");
y = Integer.parseInt( year );
m=Integer.parseInt(month);
int mon[]={29,31,28,31,30,31,30,31,31,30,31,30,31};

if (y%100==0&&y%400==0||y%100!=0&&y%4==0)
{
mon[2]=mon[0];
}
int mm=0;
if(m==1||m==2)
{
y=y-1;
mm=m+12;
}
else mm=m;
int yy,c;
int d=1,x=0,ye,w; 
c=(int)(y/100.0);
ye=y;
while(ye>=100)
{
ye=ye-100;
x=ye;
}
w=x+(x/4)+(c/4)-2*c+(26*(mm+1)/10)+d-1;
if(w<=0)
{
w=w*(-1);
}
int xq=w%7;

JTextArea outputArea = new JTextArea();
String output=" sun mon tue wed thu  fri  sta\n"+xq;
for(int tt=(-1)*xq,ttt=0;tt<= mon[m];tt++,ttt++)
{
if(tt<0)
{
output+="       ";

}
if(tt==0){}
if(tt>=1&tt<10)
{
output+="     "+tt;
if(ttt%7==0)
{
output+="\n";
}

}
if(tt>=10)
{
output+="   "+tt;
if(ttt%7==0)
{
output+="\n";
}

} }
outputArea.setText(output);
JOptionPane.showMessageDialog( null, outputArea,"万年历",JOptionPane.INFORMATION_MESSAGE );
System.exit(0);




}


}

解决方案 »

  1.   

    你不用 蔡勒公式 判断每一天的是星期几
    你只要算出,需要的打出的年份第一天是星期几就可以了下边这个是网上的一个例子
    提示:公元元年的第一天是星期日
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    这个是个万年历程序import java.io.*;
    class putout{
     public void putout(int f,int x,int y){
      int i;
      int a[]= new int[40]; 
      System.out.println("   日   一   二   三   四   五   六 "+"   "+f+"月");
      for (i=0;i<x;i++)
      {System.out.print("     "); }
      for(i=x;i<x+y;i++)
      a[i]=i-x+1;
      for(i=x;i<x+y;i++)
      {
      if ((i%7==0)&&(i>0))
         System.out.print("\n");
         if (a[i]<10)
         System.out.print("    "+a[i]);
       else System.out.print("   "+a[i]);
       }
       System.out.println("\n");
       }
    }class st{
    public static void main(String args[])throws IOException{
    putout p=new putout();
    int year,mouth,y=1,t,i;
    InputStreamReader ir;
    BufferedReader in;
    ir=new InputStreamReader(System.in);
    in=new BufferedReader(ir);
    System.out.print("请输入一个年份:");
    String s=in.readLine();
    year=Integer.parseInt(s);
    if((year%4==0 && year%100!=0)||(year%400==0))
     mouth=1;
     else 
     mouth=0;
     y=year;
     for(i=1;i<year;i++)
      {if((i%4==0 && i%100!=0)||(i%400==0))
       y++;}
        y=y%7;
        for(i=1;i<13;i++){
           switch(i){
              case 1: {p.putout(1,y,31);y=(y+31)%7;break;}
              case 2: {p.putout(2,y,28+mouth);y=(y+28+mouth)%7;break;}
              case 3: {p.putout(3,y,31);y=(y+31)%7;break;}
                    case 4: {p.putout(4,y,30);y=(y+30)%7;break;}
                    case 5: {p.putout(5,y,31);y=(y+31)%7;break;}
                    case 6: {p.putout(6,y,30);y=(y+30)%7;break;}
                    case 7: {p.putout(7,y,31);y=(y+31)%7;break;}
                    case 8: {p.putout(8,y,31);y=(y+31)%7;break;}
                    case 9: {p.putout(9,y,30);y=(y+30)%7;break;}
                    case 10: {p.putout(10,y,31);y=(y+31)%7;break;}
                    case 11: {p.putout(11,y,30);y=(y+30)%7;break;}
                    case 12: {p.putout(12,y,31);y=(y+31)%7;break;}
                }
            }
        }
    }
      

  2.   

    import java.io.*;
    class putout{
     public void putout(int f,int x,int y){
     int i;
     int a[]= new int[40]; 
     System.out.println("   日   一   二   三   四   五   六 "+"   "+f+"月");
     for (i=0;i<x;i++)
     {System.out.print("     "); }
     for(i=x;i<x+y;i++)
     a[i]=i-x+1;
     for(i=x;i<x+y;i++)
     {
     if ((i%7==0)&&(i>0))
        System.out.print("\n");
        if (a[i]<10)
        System.out.print("    "+a[i]);
       else System.out.print("   "+a[i]);
       }
       System.out.println("\n");
       }
    }class st{
    public static void main(String args[])throws IOException{
    putout p=new putout();
    int year,mouth,y=1,t,i;
    InputStreamReader ir;
    BufferedReader in;
    ir=new InputStreamReader(System.in);
    in=new BufferedReader(ir);
    System.out.print("请输入一个年份:");
    String s=in.readLine();
    year=Integer.parseInt(s);
    if((year%4==0 && year%100!=0)||(year%400==0))
     mouth=1;
     else 
     mouth=0;
     y=year;
     for(i=1;i<year;i++)
      {if((i%4==0 && i%100!=0)||(i%400==0))
       y++;}
        y=y%7;
        for(i=1;i<13;i++){
          switch(i){
            case 1: {p.putout(1,y,31);y=(y+31)%7;break;}
            case 2: {p.putout(2,y,28+mouth);y=(y+28+mouth)%7;break;}
            case 3: {p.putout(3,y,31);y=(y+31)%7;break;}
                 case 4: {p.putout(4,y,30);y=(y+30)%7;break;}
                 case 5: {p.putout(5,y,31);y=(y+31)%7;break;}
                 case 6: {p.putout(6,y,30);y=(y+30)%7;break;}
                 case 7: {p.putout(7,y,31);y=(y+31)%7;break;}
                 case 8: {p.putout(8,y,31);y=(y+31)%7;break;}
                 case 9: {p.putout(9,y,30);y=(y+30)%7;break;}
                 case 10: {p.putout(10,y,31);y=(y+31)%7;break;}
                 case 11: {p.putout(11,y,30);y=(y+30)%7;break;}
                 case 12: {p.putout(12,y,31);y=(y+31)%7;break;}
              }
           }
        }
    }
      

  3.   

    首先谢谢给我提供的程序。我在程序中是用蔡勒公式算每个月的第一天是星期几。其中例如计算2006年4月时,用w=x+[x/4]+[c/4]-2*c+(26*(m+1)/10)+d-1计算。x为6是2006年,c为20是21世纪减去1,m为4是4月,d为1是4月1号。按理说w除以7的余树应该是6,可是我笔算和程序中都得出的是1。我很费解。但是1.2.10.11.12月都没有问题。请问各位高人这是为什么,我哪里错了。谢谢
      

  4.   

    不知道你算得是哪一年,蔡勒公式,好像只是针对1582年以后的年份有效好吧,我算了一下,发现结果等于2006.4.1的w是-15,对7求余的话是-1
    就是说,是星期日的前一天,那就是星期六了
    你直接就绝对值 取正了吧,我也没有想到,居然还符号这篇文章就是星期几的事情,还有推导过程,不过你只需要用当年第一天是星期几就好了如何计算某一天是星期几?
    http://www.blog.edu.cn/user1/8722/archives/2005/332784.shtml
      

  5.   

    package MonthTest;
    import java.util.*;/**
    *打印日历
    */
    class MonthDemo{
    Calendar today;
    int month;
    int year;
    int weekday;
    int day;
    MonthDemo(){
    today=Calendar.getInstance();
    }
    void shezhi(int y,int m){
    today.clear();
    today.set(Calendar.YEAR,y);
    today.set(Calendar.MONTH,m);
    today.set(Calendar.DAY_OF_MONTH, 1);
    month=today.get(Calendar.MONTH)+1;
    year=today.get(Calendar.YEAR);
    weekday=today.get(Calendar.DAY_OF_WEEK);
    }
    void xianshi(){
    System.out.println("\t\t\t  "+year+"年"+month+"月");
    System.out.println("\t日\t一\t二\t三\t四\t五\t六");
    for(int i=0;i<weekday;i++){
    System.out.print("\t");
    }
    do{
    System.out.print(today.get(Calendar.DATE)+"\t");
    if(today.get(Calendar.DAY_OF_WEEK)==7)
    System.out.print("\n\t");
    today.add(Calendar.DAY_OF_MONTH,1);
    }while(today.get(Calendar.MONTH)==(month-1));
    }
    }
    public class MonthTest{
    public static void main(String args[]){
    int y,m;
    try{
    MonthDemo Md=new MonthDemo();
    y=Integer.parseInt(args[0]);
    m=Integer.parseInt(args[1]);
    Md.shezhi(y,m);
    Md.xianshi();
    }catch(Exception e){
    System.out.println(e.getMessage());
    }
    }
    }
    就用util包中的Calendar对象就能实现
      

  6.   

    元年元月元日就是星期一,不知道Java里有没有函数可以直接计算从某一天到某一天的间隔天数
      

  7.   

    路过深圳java程序员博客,为你提供多方面资料http://drivemewild.blogchina.com
      

  8.   

    弱弱的问下你们,为什么不用util包中的Calendar呢?
    我感觉比那样写算法来的简单。。
      

  9.   

    十分感谢DrinkT,我的万年历已经好用了,问题就是你说的,如果是负数要加7。
    也谢谢其他人,我是初学者,对于大家说的util包的使用,我还学过。今后可能还要问大家,希望各位高人到时不要吝啬。
    再次谢谢每位回帖的人!