并排输出日历(自己写的,很繁琐,没有太大技巧)

今天做题做到了这个题,经过多方辗转,最后还是选择了自己想出来的方法,虽然很笨,很繁琐,终归还是在一点点的探索中写出来了,
之后也有看过其他人写的这个题的代码,经过思考理解了,也明白了我的代码和其他人的代码之间的区别,我想写出循环来,但终究水
平不够,只能一个个的写出所谓的循环,但是,我还是在写代码的过程中收获了很多,更熟悉了for循环(因为这个代码一直在用for
循环),相信日后定能更熟练的掌握for了,不建议大家是用这种方法,这真的是最笨的方法了


package SevenTest;
import java.util.Scanner;
import java.util.Arrays;
public class calender2 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Please enter a month");
int month = scan.nextInt();
int Days1;
//知道在2019年某月份前有多少天
Days1 = getDaysFrom1900(month);
//知道这月有多少天
int days1;
days1 = getDaysFromMonth(month);
//判断一个月的第一天是星期几
int kongge = Days1 % 7;
//输出日历
int count1 = 0;

int Days2;
Days2 = getDaysFrom1900(month+1);
int days2;
days2 = getDaysFromMonth(month+1);
int kongge2 = Days2 % 7;
int count2 = 0;
System.out.println("一\t二\t三\t四\t五\t六\t日\t"+"\t\t"+"一\t二\t三\t四\t五\t六\t日\t"); //输出表头
//先输出第一行
for (int i = 0; i < kongge; i++) {
System.out.print("\t");
}//输出前面的空格
for (int i = 1; i <= 7-kongge; i++) {
System.out.print(i + "\t");
}
System.out.print("\t\t");
for(int i=0; iSystem.out.print("\t");
}
for(int i=1; i<=7-kongge2; i++)
System.out.print(i+"\t");
System.out.println();
//输出第二行
for(int i=8-kongge; i<=14-kongge; i++)
System.out.print(i + "\t");
System.out.print("\t\t");
for(int i=8-kongge2; i<=14-kongge2; i++)
System.out.print(i + "\t");
System.out.println();
//输出第三行
for (int j = 15 - kongge; j <= 21 - kongge; j++)
System.out.print(j + "\t");
System.out.print("\t\t");
for (int j = 15 - kongge2; j <=21 - kongge2; j++)
System.out.print(j + "\t");
System.out.println();
//输出第四行
for (int z = 22 - kongge; z <= 28 - kongge; z++)
System.out.print(z + "\t");
System.out.print("\t\t");
for (int z = 22 - kongge2; z <= 28 - kongge2; z++)
System.out.print(z + "\t");
System.out.println();
//输出第五行
for (int h = 29 - kongge; h <= 35 - kongge&&h<=days1; h++)
System.out.print(h + "\t");
for(int y=days1; ySystem.out.print("\t");
System.out.print("\t\t");
for (int h = 29 - kongge2; h<=days2&&h<=35-kongge2; h++)
System.out.print(h + "\t");
System.out.println();
//打印第6行
for (int h = 35 - kongge; h <= 41 - kongge&&h<=days1; h++)
System.out.print(h+"\t");
for(int y=days1; ySystem.out.print("\t");
System.out.print("\t\t");
System.out.print("\t\t\t\t\t\t");
for (int h = 36 - kongge2; h<=days2&&h<=41-kongge2; h++){
System.out.print(h + "\t"); }
System.out.println();
scan.close();
}



//写方法分别获取其到1900的天数,以及是否为闰年
public static int getDaysFrom1900 ( int month1){
int days = 0;

for (int i = 1990; i < 2019; i++) {
days += isLeapYear(i) ? 366 : 365;
}
for (int i = 1; i < month1; i++) {
days += getDaysFromMonth(i);
}
return days;
}

public static int getDaysFromMonth ( int month){
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
return 28;
default:
return 30;

}
}

public static boolean isLeapYear ( int year){
return (year % 4 == 0);


}
}

    推荐阅读