栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java语言程序设计数据结构基础篇第11版5.29 输入年以及该年的第一天是星期几从而打印出该年的日历(java )

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java语言程序设计数据结构基础篇第11版5.29 输入年以及该年的第一天是星期几从而打印出该年的日历(java )

( 显示日历)编写程序,提示用户输人年份和代表该年第一天是星期几的数字,然后在控制台上 显示该年的日历表。例如,如果用户输人年份 2013 和代表 2013 年 1 月 1 日为星期二的 2, 程 序应该显示该年每个月的日历,如下所示:

 

 

package javaBasicPart;
import java.util.Scanner;

public class showCalendar {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the year:");
        int year= input.nextInt();
        System.out.println("Please enter the first day of the year:(0 means Sunday)");
        int firstDay= input.nextInt();
        String[] month={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        for (int i=0;i<12;i++){
            int count = 0;
            System.out.println("n");
            System.out.println("ttttt" + month[i] + " " + year);
            System.out.println("-----------------------------------------------------");
            System.out.println("  Sunt "+" Mont " + " Tuet " + " Wedt " + " Thut " + " Frit " + " Sat ");

            if (firstDay == 7) {
                System.out.print(" ");
            }
            else if (firstDay == 1) {
                System.out.print(" t t");
                count += 2;
            }
            else if (firstDay == 2) {
                System.out.print(" t t t t");
                count += 4;
            }
            else if (firstDay == 3) {
                System.out.print(" t t t t t t");
                count += 6;
            }
            else if (firstDay == 4) {
                System.out.print(" t t t t t t t t");
                count += 8;
            }
            else if (firstDay == 5) {
                System.out.print(" t t t t t t t t t t");
                count += 10;
            }
            else if (firstDay == 6) {
                System.out.print(" t t t t t t t t t t t t");
                count += 12;
            }
            if (i == 1) {
                if (isLeap(year)) {
                    for (int k = 1; k <= 29; k++) {
                        if (count % 14 == 0&&count!=0)
                            System.out.println(" ");
                        System.out.print("t"+k + " t");
                        count+=2;
                    }
                    firstDay=(firstDay+29)%7;
                } else {
                    for (int k = 1; k <= 28; k++) {
                        if (count % 14 == 0&&count!=0)
                            System.out.println(" ");
                        System.out.print("t"+k + " t");
                        count+=2;

                    }
                    firstDay=(firstDay+28)%7;

                }
            }

            if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) {
                for (int k = 1; k <= 31; k++) {
                    if (count % 14 == 0&&count!=0)
                        System.out.println(" ");
                    System.out.print("t"+k + " t");
                    count+=2;

                }
                firstDay=(firstDay+31)%7;

            }

            if (i == 3 || i == 5 || i == 8 || i == 10) {
                for (int k = 1; k <= 30; k++) {
                    if (count % 14 == 0&&count!=0)
                        System.out.println(" ");
                    System.out.print("t"+k + " t");
                    count+=2;
                }
                firstDay=(firstDay+30)%7;

            }




        }

    }
    public static boolean isLeap(int year){
        //判断是否是闰年
        return (year%4==0&&year%100!=0)||(year%400==0);
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/768770.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号