首页 > 编程知识 正文

java实现任意年份的日历打印(java打印万年历)

时间:2023-12-24 12:05:52 阅读:321120 作者:GMHL

本文目录一览:

输入年份月份,打印输出当月日历 用java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class java {

public static void main(String[] args) throws IOException {

BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));

System.out.println("请输入年份:");

String s1=buf.readLine();

System.out.println("请输入月份: ");

String s2=buf.readLine();

int year=Integer.parseInt(s1);

int month=Integer.parseInt(s2);

switch(month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 13:

System.out.println("在"+year+"年的"+month+"月有:31天");

break;

case 4:

case 6:

case 9:

case 11:

System.out.println("在"+year+"年的"+month+"月有:30天");

break;

case 2:

if(year%4==0year%100!=0||year%400==0)

{

System.out.println("在"+year+"年的"+month+"月有:29天");

}

else

{

System.out.println("在"+year+"年的"+month+"月有:28天");

}

break;

}

}

}

编写java程序,由键盘输入年份,显示当年日历,要求使用数组。

首先键盘输入的话多种方式实现,提供两种,一是使用main方法的arg参数,也就是在使用java命令的时候后面跟上参数,在main方法里调用arg[0]即可,第二种方法是使用scanner类来得到system.in的内容

然后就是打印全年,这个只要使用calendar类即可,得到calendar对象以后通过一个循环365次,不断的day加一即可

Java编写程序,输入年份,输出本年度各月份日历

写了个简明的,

import java.util.Calendar;

import java.util.Scanner;

public class Test {

static public void main(String 参数[]){

Calendar c = Calendar.getInstance();

Scanner sc = new Scanner(System.in);

System.out.println("请输入年份:");

int year= sc.nextInt();

c.set(Calendar.YEAR, year);

c.set(Calendar.MONTH, Calendar.JANUARY);

c.set(Calendar.DAY_OF_MONTH, 1);

while(c.get(Calendar.YEAR)==year){

int wday=c.get(Calendar.DAY_OF_WEEK);

int mday=c.get(Calendar.DAY_OF_MONTH);

if(mday==1){

System.out.println("n日t一t二t三t四t五t六t第"+(c.get(Calendar.MONTH)+1)+"月");

System.out.println("---------------------------------------------------");

for(int i=0;iwday-1;i++) System.out.print(" t");

}

System.out.print(mday+"t");

if(wday==7) System.out.println();

c.add(Calendar.DAY_OF_YEAR, 1);

}

}

}

=======

请输入年份:

2012

日 一 二 三 四 五 六 第1月

---------------------------------------------------

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

日 一 二 三 四 五 六 第2月

---------------------------------------------------

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29

日 一 二 三 四 五 六 第3月

---------------------------------------------------

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

日 一 二 三 四 五 六 第4月

---------------------------------------------------

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

日 一 二 三 四 五 六 第5月

---------------------------------------------------

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

日 一 二 三 四 五 六 第6月

---------------------------------------------------

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

日 一 二 三 四 五 六 第7月

---------------------------------------------------

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

日 一 二 三 四 五 六 第8月

---------------------------------------------------

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30 31

日 一 二 三 四 五 六 第9月

---------------------------------------------------

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30

日 一 二 三 四 五 六 第10月

---------------------------------------------------

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

日 一 二 三 四 五 六 第11月

---------------------------------------------------

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30

日 一 二 三 四 五 六 第12月

---------------------------------------------------

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31

输入某年某月 打印出当月日历,要求日期和星期对应,用JAVA语言

import java.util.*;

public class riqi {

public static void main(String[] args) {

int totaldays = 0;

int dayofmonth = 0;

int twomonth;

Scanner input = new Scanner(System.in);

System.out.print("请输入年份:");

int year = input.nextInt(); // 输入年份

System.out.print("请输入月份:");

int month = input.nextInt(); // 输入月份

for (int i = 1900; i year; i++) {

if (((i % 4 == 0) (i % 100 != 0)) || (i % 400 == 0)) {

totaldays = totaldays + 366;

} else {

totaldays = totaldays + 365;

}

}

for (int nomonth = 1; nomonth = month; nomonth++) {

if (nomonth == 1 || nomonth == 3 || nomonth == 5 || nomonth == 7

|| nomonth == 8 || nomonth == 10 || nomonth == 12) {

dayofmonth = 31;

} else if (nomonth == 2) {

if ((year % 4 == 0) (year % 100 != 0) || (year % 400 == 0)) {

dayofmonth = 29;

} else {

dayofmonth = 28;

}

} else {

dayofmonth = 30;

}

if (nomonth month) {

totaldays = totaldays + dayofmonth;

}

// System.out.println(totaldays);

}

System.out.println("星期一t星期二t星期三t星期四t星期五t星期六t星期天");

int temp = (totaldays % 7);

// System.out.print(temp);

for (int p = 0; p temp; p++) {

System.out.print("t");

}

for (int w = 1; w = dayofmonth; w++) {

System.out.print(w + "t");

if ((totaldays + w) % 7 == 0) {

System.out.println();

}

}

}

}

Java日历打印,怎么加输入年份,打印整年12个月打印,输入月份打印季节,下面代码如何优化,给个思路?

建议封装函数,使用数组完成所有的打印

比如获取天数,getDays(int year,int month)

获取星期,getDay(int allDays)

获取极度,getQuater(int month)

然后打印重载写print方法,传入的flag不同打印年 嫉妒等

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。