Calendar in a nested loop using java [duplicate]

一曲冷凌霜 提交于 2021-01-29 00:36:36

问题


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).

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

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   

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?


回答1:


You can do like this

    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++;
    }
}



回答2:


you need to add some spaces according to days of a month:

int year = 2020;
int startDayofYear = 3; // Wednesday is the start day of 2020
int dayCounter = startDayofYear;
int nbrOfDays = 0;
String monthName = "";

for (int month = 1; month <= 12; month++) { 
    // Switch to chose the month
    switch (month) {
    case 1:
        monthName = "January";
        nbrOfDays = 31;
        break;
    case 2:
        monthName = "February";
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            nbrOfDays = 29;
            break;
        } else {
            nbrOfDays = 28;
            break;
        }
    case 3:
        monthName = "March";
        nbrOfDays = 31;
        break;
    case 4:
        monthName = "April";
        nbrOfDays = 30;
        break;
    case 5:
        monthName = "May";
        nbrOfDays = 31;
        break;

    case 6:
        monthName = "June";
        nbrOfDays = 30;
        break;
    case 7:
        monthName = "July";
        nbrOfDays = 31;
        break;
    case 8:
        monthName = "August";
        nbrOfDays = 31;
        break;
    case 9:
        monthName = "September";
        nbrOfDays = 30;
        break;
    case 10:
        monthName = "October";
        nbrOfDays = 31;
        break;
    case 11:
        monthName = "November";
        nbrOfDays = 30;
        break;
    case 12:
        monthName = "December";
        nbrOfDays = 31;
        break;
    }

    System.out.printf("%15s %d  \n", monthName, year);
    System.out.println("----------------------------");
    System.out.printf("%s %s %s %s %s %s %s\n ", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

    for (int space = 1; space <= startDayofYear; space++) {
        System.out.printf("%4s", "    ");
    }
    for (int i = 1; i <= nbrOfDays; i++) {
        dayCounter++;
        if (dayCounter % 7 == 0)
            System.out.printf("%- 4d\n", i);
        else
            System.out.printf("%-4d", i);

    }
    startDayofYear = (startDayofYear + nbrOfDays) % 7;
    System.out.println();
}


来源:https://stackoverflow.com/questions/64763790/calendar-in-a-nested-loop-using-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!