Import Java.util.Scanner;
public class Perpetualcalendar {
public static void Main (string[] args) {
TODO auto-generated Method Stub
int year;//Year
int month;//Month
int day=0;//Days
Boolean ren;//is a leap year to true, not false
int totaldays=0;//1900 years to present total number of days
System.out.println ("***************** calendar *****************");
Scanner input = new Scanner (system.in);
System.out.println ("Please enter the Year:");
Year = Input.nextint ();
System.out.println ("Please enter the month:");
month = Input.nextint ();
if ((year%400==0) | | | (year%4==0&&year%100!=0)) {
ren = true; }
else {ren = false;
}
Calculate the total number of days from 1900 to this year
for (int i = 1900;i<year;i++) {
if (ren) {
Totaldays = totaldays+366;
}else {
Totaldays = totaldays+365;
}
}
for (int i = 1;i<=month;i++) {
switch (i) {
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:
Day = 31;
Break
Case 4:
Case 6:
Case 9:
Case 11:
day=30;
Break
Case 2:
if (ren) {
Day = 29;
}
else{
Day = 28;
}
Break
Default
System.out.println ("Incorrect month input");
}
To make day a value equal to the number of days to find a month, it is not added to the total number of days
if (i!=month) {
Totaldays = number of days in total years of totaldays+day;//plus number of days before month of lookup month
}
}
Ask for the day of the week, the number of the front \ t
int befordays;
Befordays = 1 + totaldays%7;
if (befordays = = 7) {
Befordays = 0;//stands for Sunday, 0 \ t
}
System.out.println ("Sunday \ t Monday \ t Tuesday \ t Wednesday \ Thursday \ t Friday \ t Saturday");
First print \ t
for (int i=0;i<befordays;i++) {
System.out.print ("\ t");
}
for (int i=1;i<=day;i++) {
System.out.print (i+ "\ t");
Full Seven line breaks
if ((i+befordays)%7==0) {
System.out.println ();
}
}
System.out.println (month+ "Month has" +day+ "Day");
}
}
Java Perpetual calendar