Stuck on a Calendar Loop Program in Java

Hey everyone! I am new to posting in the forums but I had a question about a homework program for my college programming class I am in. I am suppose to write a program that prints out a calendar for each month of the year depending on what day of the week that year starts on. Everything works except for the last little part that prints the numbers out. I found a solution online for it but it doesn’t help me if I don’t know how it works. I’ve tried recreating it using different styles of loops but its not working. I mainly need to figure out why this loop works to begin with and then I can create my own version of it. I told my professor I had found how to do the last part of the program online and didn’t feel right about turning it in since I didn’t know how it worked,so he gave me an extension and said figure out how the loop works then rewrite it yourself using your own version. I don’t need a full explanation,just maybe a good point in the right direction,cause I am completely lost. Thanks!

I’ve uploaded a picture of my code. The two for-loops at the bottom is what I need help on. The greyed out while-loops were my attempt at recreating it and failing.

this is my full code in text edit form:

import java.util.Scanner;
import javax.swing.JOptionPane;
public class Ex529 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        String yearTxt = JOptionPane.showInputDialog
        ("Enter a year");
        int year = Integer.parseInt(yearTxt);
        
        String dayTxt = JOptionPane.showInputDialog
        ("Enter a Number Correlating to \na day in the week that is "
                + "\nthe first day of that year. "
                + "\nI.e. 0 for Sunday,1 for Monday,etc.");
        int day = Integer.parseInt(dayTxt);
        int monthdays = 0;
        
        String m1 = "January",m2 = "February",m3 = "March",m4 = "April",
                m5 = "May",m6 = "June",m7 = "July",m8 = "August",
                m9 = "September",m10 = "October",m11 = "November",
                m12 = "December";
        
        for(int mon = 1;mon < 13;mon++){
            switch(mon){
                case 1:
                    System.out.println("\t   " + m1 + " " + year);
                    monthdays = 31;
                    break;
                case 2:
                    System.out.println("\t   " + m2 + " " + year);
                    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                        monthdays = 29;
                    }else{
                        monthdays = 28;
                    }break;
                case 3:
                    System.out.println("\t     " + m3 + " " + year);
                    monthdays = 31;
                    break;
                case 4:
                    System.out.println("\t     " + m4 + " " + year);
                    monthdays = 30;
                    break;
                case 5:
                    System.out.println("\t     " + m5 + " " + year);
                    monthdays = 31;
                    break;
                case 6:
                    System.out.println("\t     " + m6 + " " + year);
                    monthdays = 30;
                    break;
                case 7:
                    System.out.println("\t     " + m7 + " " + year);
                    monthdays = 31;
                    break;
                case 8:
                    System.out.println("\t     " + m8 + " " + year);
                    monthdays = 31;
                    break;
                case 9:
                    System.out.println("\t     " + m9 + " " + year);
                    monthdays = 30;
                    break;
                case 10:
                    System.out.println("\t     " + m10 + " " + year);
                    monthdays = 31;
                    break;
                case 11:
                    System.out.println("\t     " + m11 + " " + year);
                    monthdays = 30;
                    break;
                case 12:
                    System.out.println("\t     " + m12 + " " + year);
                    monthdays = 31;
                    break;
            }//end of switch
            
            System.out.println("========================================");
            System.out.println("Sun   Mon   Tue   Wed   Thu   Fri   Sat");
            
            int week1 = 1;
            int week2 = 1;
            
//            while(week1 <=day){
//                System.out.println("      ");
//                week1++;
//            }
//            while(week2 <= monthdays){
//                if(day % 7 == 0 && day!= 0){
//                    System.out.println("");
//                }
//                System.out.printf("%3d   ",week2);
//                day += 1;
//                week2++;
//            }
//            day %= 7;
//            
//            System.out.println("\n\n");
            
            for(; week1 <= day; week1++) {
                System.out.print("      ");
            }
            for(; week2 <= monthdays; week2++) {
                if (day % 7 == 0 && day != 0) {
                    System.out.println();
                }
                System.out.printf("%3d   ", week2);
                day += 1;
            }
            day %= 7;

            System.out.print("\n\n\n");
        
        }//end of for-loop
        
        
    }//end of main
}//end of class

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

thank you so much for that! I was wondering why it was disconnected like that