使用Java的嵌套循环中的日历 [英] Calendar in a nested loop using java

查看:70
本文介绍了使用Java的嵌套循环中的日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名高中学生,我很难使用嵌套循环在日历的第一行中添加空格.由于尚未了解数组,因此无法使用数组(这是我们的下一个主题).

I am a Senior High School student and I'm having a hard time how to add a white spaces in the first row of my calendar using nested loops. We can't use arrays becauce we haven't learned about it yet(it is our next topic).

我们的老师要求我们使用Java中的嵌套循环创建2020年日历.用户需要使用数字值(1-12)输入月份.并且需要显示输入月份2020年的相应日历.

Our teacher asked us to create a 2020 calendar using nested loops in java. The user need to enter a month using a numerical value (1-12). and we need to display the corresponding calendar for the year 2020 of the entered month.

这是我到目前为止的代码:

Here is my code as of now:

Scanner s = new Scanner(System.in);

System.out.println("Enter month[1-12]");
int month = s.nextInt();


System.out.println("Sun\tMon\tTues\tWed\tThur\tFri\tSat");
    switch(month)
    {
        case 1: //1 stands for the month of January 2020
        for(int i = 1; i <= 31; i++)
        {
            System.out.print(i+ "\t");
            if(i%7 == 0)
            {
            System.out.println();
            }
        }
    }

这是我的代码的当前输出:

This is the current output of my codes:

 Sun   Mon   Tue    Wed   Thu   Fri  Sat
  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

这是2020年1月我们计划中所需的示例输出:

This the needed sample output on our program in January 2020:

   January 2020
   Sun  Mon Tue  Wed  Thu   Fri  Sat
                  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月份的示例输出

EDITED: And this is the sample output on our program in the month of February

   February 2020
   Sun  Mon Tue  Wed  Thu   Fri  Sat
                                 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  

有什么想法可以到达那里吗?

Any idea to get there please?

推荐答案

您可以这样做

    public static void main(String[] args) throws IOException {
    int sumDay = 0;
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the year:");
    int year = in.nextInt();
    System.out.print("Please enter month:");
    int month = in.nextInt();
    System.out.println(year + "year" + month + "month:");
    for (int i = 1900; i < year; i++) {
        if ((i % 4 == 0) && (i % 100 != 0) || (i % 400 == 0)) {
            sumDay += 366;
        } else {
            sumDay += 365;
        }
    }

    for (int j = 1; j < month; j++) {
        if (j == 4 || j == 6 || j == 9 || j == 11) {
            sumDay += 30;
        } else if (j == 2) {
            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
                sumDay += 29;
            } else {
                sumDay += 28;
            }
        } else {
            sumDay += 31;
        }
    }
    /*The above code calculates the total number of days from January 1, 1900 to the entered year and month. If 2020 and 10 are entered, the number of days from 1900.1.1 to 2020.9.30 will be calculated*/

    /*xingQi is used to calculate the day of the week on the 1st of the entered month*/
    int xingQi = (sumDay + 1) % 7;

    int everydayXingQi = sumDay + 1;
    /*monthDay is used to receive the days of the month*/
    int monthDay;
    System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");

    if (month == 4 || month == 6 || month == 9 || month == 11) {
        monthDay = 30;
    } else if (month == 2) {
        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
            monthDay = 29;
        } else {
            monthDay = 28;
        }
    } else {
        monthDay = 31;
    }

    /*Output the space before the 1st of each month*/
    for (int i = 0; i < xingQi; i++) {
        System.out.print("\t");
    }
    /*Output the number of days in each month and control line break*/
    for (int i = 1; i <= monthDay; i++) {
        if (everydayXingQi % 7 == 6) {
            System.out.print(i + "\n");
        } else {
            System.out.print(i + "\t");
        }
        everydayXingQi++;
    }
}

这篇关于使用Java的嵌套循环中的日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆