⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monthcalendar.java

📁 是<java程序设计>的课后作业2-8源码.可以供初学者参考,作了解java基础语法所用.
💻 JAVA
字号:
/**
 * @(#)MonthCalendar.java
 *
 *
 * @author 
 * @version 1.00 2009/3/3
 */
import javax.swing.JOptionPane;
public class MonthCalendar {
        
    /**
     * Creates a new instance of <code>MonthCalendar</code>.
     */
    public MonthCalendar() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //Enter the year and the first day's position in a week
        String stringYear=JOptionPane.showInputDialog(null,
        "Please input a year, for example,2009","Execise3.34 input",JOptionPane.QUESTION_MESSAGE);
        String stringFirstDayInWeek=JOptionPane.showInputDialog(null,
        "Please input the day of the first day in the year\n"+ 
        "in a week(the first three letter of the word.), for example,Sun",
        "Execise3.34 input",JOptionPane.QUESTION_MESSAGE);
        
        //Convert string into int
        int year=Integer.parseInt(stringYear);
        // The year is leap year?
        boolean isLeapYear=(((year%4==0)&&(year%100!=0))||(year%400==0))?true:false;
        
        //Input the week name and month name into arrays
        String[] WeekName={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
        String[] MonthName={"January","February","March","April","May","June",
        "July","August","September","October","November","December"}; 
        	
        //Get the week day index of the first day in the year in 
        int numberInWeek=0;
        for(;;numberInWeek++)
        	if(stringFirstDayInWeek.equalsIgnoreCase(WeekName[numberInWeek]))
        		break;
        		
         //打印一年的月历
        int i=1;
        for(i=1;i<=12;i++)
        {
        	//Days in each month
        	int NumberOfDays=0;        	
        	switch(i)
        	{
        		case (1):case (3):case (5):case (7):
        		case (8):case (10): case (12): 
        			NumberOfDays=31;break;//这里的break只是跳出了switch块,不是跳出循环
        		case (4):case (6):case (9):case (11):
        			NumberOfDays=30;break;
        		case (2):
        			NumberOfDays=(isLeapYear)?29:28;break;
        	}
        	
        	//打印月份,年份
        	System.out.println("        "+MonthName[i-1]+"  "+year);
        	//打印分界线
        	System.out.println("  ---------------------------");
        	//保证对齐,每行开头空两格
        	System.out.print("  ");
        	//打印星期日~~~星期六,每个单词后空一格
        	for(int j=0;j<7;j++)
        	System.out.print(WeekName[j]+" ");
        	//换行,以准备打印日期
        	System.out.print("\n");
        	
        	//打印每月第一周里空的
        	for(int j=0;j<numberInWeek;j++)
        		System.out.print("    ");
        		
        	//开始打印日期,注意换行
        	for(int j=1;j<=NumberOfDays;j++)
        	{
        		//两位数空2格,一位数空3格
        		if(j>9)
        			System.out.print("  "+j);
        		else
        			System.out.print("   "+j);
        		
        		//如果打完了周六,九换行.		
        		if((j+numberInWeek)%7==0)
        			System.out.print("\n");        		   
        	}
        	
        	//每个月打印完,换行
        	System.out.print("\n\n");
        	
        	//记录下个月的第一天是周几
        	numberInWeek=(numberInWeek+NumberOfDays)%7;
        //	System.out.println("This is not passed!");
        }
        System.out.println("\n");
        return;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -