java 打印 日历 详细 注解_Java打印日历

描述

已知2020年第一天是星期日 ,打印2020年某月的月历。

输入

输入月份

输出

输出提示:

System.out.printf("%4s%4s%4s%4s%4s%4s%4s","日","一","二","三","四","五","六");

System.out.println("*****2020年"+month+"月份*****");

System.out.printf("%3d",day);

难度

入门

输入示例

2

输出示例

*****2020年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

完成代码:

import java.util.Scanner;

public class Demo02 {

public static void main(String[] args) {

Scanner in=new Scanner(System.in);

int month=in.nextInt();

print(month);

}

public static int day(int i){

int k=-1;

switch (i){

case 1: case 3: case 5: case 7: case 8: case 10: case 12:k=31;break;

case 4: case 6: case 9: case 11:k=30;break;

case 2:k=29;break;

default:break;

}

return k;

}

public static void print(int month){

int sum=3;

int k=day(month);

for (int j = 1; j < month; j++)

sum+=day(j);

int s=sum%7;

System.out.println("*****2020年"+month+"月份*****");

System.out.printf("%4s%4s%4s%4s%4s%4s%4s","日","一","二","三","四","五","六");

System.out.println();

int n=1;

for (int i = 0; i < s; i++)

System.out.printf("%3s"," ");

for (;s<7;s++){

System.out.printf("%3d",n);

n++;

}

System.out.println();

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 7; j++) {

System.out.printf("%3d",n);

n++;

if (n>k)break;

}

System.out.println();

}

}

}

标签:case,日历,Java,int,打印,System,month,4s%,out

来源: https://www.cnblogs.com/Blogwjl/p/12346825.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值