谢谢大家的支持,您的一键三连是 罡罡同学前进的最大动力!
一键三连 一键三连 一键三连 一键三连 一键三连 一键三连
Java 任意年月日历输出 算法提高
import java.util.Scanner;
public class Main {
public static boolean isLeap(int year) {
boolean flag = false;
if(year % 4 == 0 && year % 100 != 0)
flag = true;
else if(year % 400 == 0)
flag = true;
else
flag = false;
return flag;
}
public static int getDay(int month, int year) {
int day = 0;
if (month == 2) {
if (isLeap(year))
day = 29;
else
day = 28;
} else if (month < 8) {
if(month % 2 == 0)
day = 30;
else
day = 31;
} else {
if(month % 2 == 0)
day = 31;
else
day = 30;
}
return day;
}
public static int getFirst(int month, int year) {
int first = 0;
int distance = year - 2007;
int day = 0;
while (distance / 4 >= 1) {
day += (365 * 4 + 1);
distance -= 4;
}
int count = 0;
while (distance > 0) {
if (count == 1)
day += 366;
else
day += 365;
count++;
distance--;
}
int fre = 0;
if(isLeap(year))
fre = 31 + 29;
else
fre = 31 + 28;
int mon[] = {0, 0, 31, fre, fre+31, fre+61, fre+92, fre+122, fre+153, fre+184, fre+214, fre+245, fre+275};
day += mon[month];
first = day % 7;
return first+1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();
sc.close();
if(year < 2007)
return;
int day = getDay(month, year);
int firstDay = getFirst(month, year);
if(month < 10)
System.out.println("Calendar " + year + " - 0" + month);
else
System.out.println("Calendar " + year + " - " + month);
for (int i = 0; i < 21; i++)
System.out.print("-");
System.out.print("\n");
System.out.println("Su Mo Tu We Th Fr Sa ");
for (int i = 0; i < 21; i++)
System.out.print("-");
System.out.print("\n");
int count = 0;
if(firstDay != 7)
for (int i = 0; i < firstDay; i++) {
System.out.print(" ");
count++;
}
for (int i = 1; i <= day; i++) {
if(i < 10)
System.out.print(" " + i + " ");
else
System.out.print(i + " ");
count++;
if(count % 7 == 0)
System.out.print("\n");
}
System.out.print("\n");
for (int i = 0; i < 21; i++)
System.out.print("-");
}
}
谢谢大家的支持,您的一键三连是 罡罡同学前进的最大动力!
一键三连 一键三连 一键三连 一键三连 一键三连 一键三连