代码改变世界

打印万年历

2017-09-26 21:56  #qrr#  阅读(282)  评论(0)    收藏  举报

 

package 项目1;
import java.util.Scanner;


public class wannl {
//@SuppressWarnings("null")
public static void main(String[] args){

int a[][]=new int[6][7];
int n,y,t,xq;
System.out.print("Please enter a year:\n");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
System.out.print("Please enter a month:\n");
Scanner sc1=new Scanner(System.in);
y=sc1.nextInt();
int yt=jsts(n,y);
System.out.print("yt="+yt+"\n");
t=zts(n,y);
xq=xingqi(t);


int b=1;
//日历存入二维数组
for(int i=0;i<xq;i++)
a[0][i]=0;
for(int i=xq;i<7;i++){
a[0][i]=b;
b=b+1;
}
for(int i=1;b<=yt;i++){
for(int j=0;j<7;j++,b++){
if(b<=yt)
a[i][j]=b;
else
a[i][j]=0;
}
}
System.out.print("该年该月的日历如下:\n");
System.out.println("周一\t周二\t周三\t周四\t周五\t周六\t周日");
//日历从二维数组中输出
int c=1;

for(int i=0;;i++){
if(c<=yt){
for(int j=0;j<7;j++){
if(a[i][j]==0)
System.out.print(" "+"\t");
else{
System.out.print(+a[i][j]+"\t");
c=c+1;
}
}
System.out.println("");
}

else
break;
}
}
//某天是星期几
public static int xingqi(int t){
int xq=t%7;
switch(xq)
{
case 0:return 0;
case 1:return 1;
case 2:return 2;
case 3:return 3;
case 4:return 4;
case 5:return 5;
case 6:return 6;

}
return 100;
}

//计算某年某月前距离1900年1月1日的总天数
public static int zts(int n,int y){
int p,t=0,y2;
y2=y;
int pn=0,rn=0,y1=0;
rn=(n-1-1900+2)/4;
pn=n-1900-rn;
do{
y=y-1;
y1=y1+jsts(n,y);

}while(y==1);
/*System.out.println("y1="+y1);
System.out.println("pn="+pn);
System.out.println("rn="+rn);*/
t=rn*366+pn*365+y1;
System.out.println(""+n+"年"+y2+"月前距离1900年1月1日的总天数为:"+t);
// System.out.println("该年该月前距离1900年1月1日的总天数为:"+t);
return t;
}

//判断某年某月多少天
public static int jsts(int n,int y){
int p;
/*int n,y,p;
System.out.println("Please enter a year:\n");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
System.out.println("Please enter a month:\n");
Scanner sc1=new Scanner(System.in);
y=sc1.nextInt();*/
if(y==1||y==3||y==5||y==7||y==8||y==10||y==12){

System.out.println("This year and in this month have 31 days!\n");
return 31;
}
else if(y==4||y==6||y==9||y==11){

System.out.println("This year and in this month have 30 days!\n");
return 30;
}
else if(y==2){
p=pdyn(n);
if(p==1){

System.out.println("This year and in this month have 29 days!");
return 29;
}
else{

System.out.println("This year and in this month have 28 days!");
return 28;
}
}
else return 0;
}
//判断闰年
public static int pdyn(int nian){
// int nian;
//while(true){
//System.out.println("Please enter a year:\n");

if((nian%400==0)||(nian%100!=0&&nian%4==0)){
System.out.println("This year is a leap year!");
return 1;
}
else{
System.out.println("This year is a nonleap year!");
return -1;
}
}

}