您的位置:首页 > 编程语言 > Java开发

简单日历的制作 java

2017-12-16 15:21 302 查看
下面是本人学习的成果跟大家分享如何制作简单的日历,代码如下:

import java.util.Scanner;

public class A4_25{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入年份:");
int year=input.nextInt();
System.out.println("请输入月份:");
int month=input.nextInt();
int sum=0;
for(int beforeyear=1900;beforeyear<year;beforeyear++) {
if(beforeyear%4==0&&beforeyear%100!=0||beforeyear%400==0) {
sum+=366;
}else {
sum+=365;
}
}
for(int beforemonth=1;beforemonth<month;beforemonth++) {
if(beforemonth==2) {
if(year%4==0&&year%100!=0||year%400==0) {
sum+=29;
}else {
sum+=28;
}
}else if(beforemonth==4||beforemonth==6||beforemonth==9||beforemonth==11) {
sum+=30;
}else {
sum+=31;
}
}
sum+=1;
int wekday=sum%7;
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=0;i<wekday;i++) {
System.out.print("\t");
}
int monthday=0;
if(month==2) {
if(year%4==0&&year%100!=0||year%400==0) {
monthday=29;
}else {
monthday=28;
}
}else if(month==4||month==6||month==9||month==11) {
monthday=30;
}else {
monthday=31;
}
for(int i=1;i<=monthday;i++) {
if(sum%7==6) {
System.out.print(i+"\n");
}else {
System.out.print(i+"\t");
}
sum++;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: