大家好,又见面了,我是你们的朋友全栈君。
万年历:输入要查看的年份,输出该年份的日历
实现代码:
package CJF;
import java.util.*;
public class ks {
public static void main(String[] args) {
int x = 3,n,m,y,r,k;
System.out.println("******您好,2016年华人款万年历,枫枫为您服务******");
System.out.println("**********************请输入查找年份********");
System.out.println();
Scanner input = new Scanner(System.in);
n = input.nextInt();
int cut = 365 * 3 + 366;
if(n >= 1997){
m = n - 1997;
int a = m / 4;
int b = m % 4;
x = (x + cut * a + b * 365) % 7;
}
else{
m = 1996 - n;
x =(366 - x + 2) % 7; // 先转换为1996年为基准年份
int a = m / 4;
int b = m % 4;
cut = (cut * a + b * 365) % 7;
x = (x - cut + 7) % 7;
}
if(x == 0) x = 7;
for(y = 1 ; y <= 12 ; y++){
if(y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12)
k = 31;
else if(y == 2){
if(n % 400 == 0 || (n % 4 == 0 && n % 100 != 0))
k = 29;
else
k = 28;
}
else
k = 30;
System.out.println();
System.out.println("********** " + n + " 年 " + y + " 月 " + "**********");
System.out.println("Mon Tue Wen Thu Fri Sat Sun");
for(int i = 1 ; i < x ; i++)
System.out.print(" ");
for(r = 1; r <= k; r++,x++){
if(r <= 9)
System.out.print(r + " ");
else
System.out.print(r + " ");
if(x % 7 == 0){
System.out.println();
x = 0;
}
}
System.out.println();
}
System.out.println("******本次服务到处结束,枫枫期待与您的下次会面******");
}
}
对JAVA来讲一个main方法写下了总感觉不太合适,把代码格式稍微改下层次和结构更清晰了点~
实现代码:
package CJF;
import java.util.*;
public class ks {
public static void main(String[] args) {
System.out.println("******您好,2016年华人款万年历,枫枫为您服务******");
System.out.println("**********************请输入查找年份********");
System.out.println();
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int x = Nf(n); // 确认该年第一天是星期几
if(x == 0) x = 7;
for(int y = 1 ; y <= 12 ; y++){
int k = Yf(n,y); // 判断该年该月有多少天
System.out.println();
System.out.println("********** " + n + " 年 " + y + " 月 " + "**********");
System.out.println("Mon Tue Wen Thu Fri Sat Sun");
x = R(x,k); // 调整日的输出格式
}
System.out.println("******本次服务到处结束,枫枫期待与您的下次会面******");
}
public static int Nf(int n){ // 确认该年第一天是星期几
int m,x = 3;
int cut = 365 * 3 + 366;
if(n >= 1997){
m = n - 1997;
int a = m / 4;
int b = m % 4;
x = (x + cut * a + b * 365) % 7;
}
else{
m = 1996 - n;
x =(366 - x + 2) % 7; // 先转换为1996年为基准年份
int a = m / 4;
int b = m % 4;
cut = (cut * a + b * 365) % 7;
x = (x - cut + 7) % 7;
}
return x;
}
public static int Yf(int n,int y){ // 判断该年该月有多少天
int k = 0;
if(y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12)
k = 31;
else if(y == 2){
if(n % 400 == 0 || (n % 4 == 0 && n % 100 != 0))
k = 29;
else
k = 28;
}
else
k = 30;
return k;
}
public static int R(int x,int k){ // 调整日的输出格式
for(int i = 1 ; i < x ; i++)
System.out.print(" ");
for(int r = 1; r <= k; r++,x++){
if(r <= 9)
System.out.print(r + " ");
else
System.out.print(r + " ");
if(x % 7 == 0){
System.out.println();
x = 0;
}
}
System.out.println();
if(x == 0) x = 7;
return x;
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/127925.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...