时隔两年再写博文(开的小号为了重新展示我菜来实现让别人意识到有人比他们菜的人生价值)
在c++全还回去的情形下初学Java
贴个代码先?(Markdown要一点也不会用了
写了个打印日历
迈出的第一步,纪念一下
package a;
//------------------打印日历
import java.util.Scanner;
public class Calendar1{
public static int which_day(int x){
//--------------------------------------------------------------------------------------------------返回x年1月1号周几
int y=x,days=0,flag;
if(x<2019) {
y=2019;
flag=-1;
}
else {
x=2019;
flag=1;
}
for(int i=x;i<y;i++){
if(isrun(i)) days+=366;
else days+=365;
}
return ((2+days*flag)%7+7)%7;
}
public static boolean isrun(int x){
if(x%100 == 0){
if(x%400 == 0) return true;
else return false;
}
else{
if(x%4 == 0) return true;
else return false;
}
}
public static void print_title(int k){
if(k==0) System.out.println(" Jan Feb Mar ");
if(k==1) System.out.println(" Apr May Jun ");
if(k==2) System.out.println(" Jul Aug Sep ");
if(k==3) System.out.println(" Oct Nov Dec ");//---------------输出各月表头
System.out.println("日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六 ");
}
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int year,date[]=new int[3],day[]=new int[3];
int month[][]=new int[4][4];
System.out.print(" 年份:");
year=scan.nextInt();
System.out.println();
month[0][0]=month[0][2]=month[1][1]=month[2][0]=month[2][1]=month[3][0]=month[3][2]=31;
month[0][1]=isrun(year)?29:28;
month[1][0]=month[1][2]=month[2][2]=month[3][1]=30;//-----------------------------------------------------打表各月天数
day[2]=(((which_day(year)-1)%7)+7)%7;//------day[2]是为了循环里给day[0]赋值
for(int k=0;k<4;k++){
print_title(k);
date[0]=date[1]=date[2]=1;
day[0]=(day[2]+1)%7;
day[1]=(day[0]+month[k][0])%7;
day[2]=(day[1]+month[k][1])%7;
while(date[0] <= month[k][0] || date[1] <= month[k][1] || date[2]<= month[k][2]){
for(int i=0;i<3;i++){
if(date[i]<=month[k][i]){
if(date[i]==1){
for(int j=0;j<day[i];j++)
System.out.print(" ");//月头空格
}
while(date[i]<=month[k][i] && day[i]<=6){
if(date[i]<10) System.out.print(" ");
System.out.print(date[i]+" ");
date[i]++;
day[i]++;//-------------------打印当前月当前行
}
if(date[i]==month[k][i]+1){
for(int j=day[i];j<7;j++)
System.out.print(" ");//月尾空格
}
if(day[i]==7 || date[i]==month[k][i]+1) System.out.print(" ");
}
else{
for(int j=0;j<=7;j++)
System.out.print(" ");//处理前面的月份打印完,但是后面月份未打印完的情况
}
}
System.out.println("\n");
day[2]--;
for(int j=0;j<3;j++) day[j]=(day[j]+7)%7;
}
}
}
}