我的目标是打印日历(1月2月3月,然后是4月5月6月的单独行等)1月是唯一完美工作的月份。我的问题是:1月之后的每个月的开始日(如果Jan在星期一结束,2月必须在星期二开始),并且每个月最后一周的日期没有正确放置。另一个小问题是代码的长度,对于我想要做的事情来说,它似乎太冗长/多余。我仅限于选择结构,循环和模块。任何帮助或提示,表示赞赏。在Java中打印3x4日历(1月之后的开始日期发布)
import javax.swing.JOptionPane;
public class Assignment4
{
public static int startDates(int day, int month)
{
//calculate start dates for Feb-Dec, Based on previous months end day,possibly dependant on lastDayM
//status: not working
int i;
int startDay = 7-day;
return startDay;
}
public static int lastDayM(int month, int year)
{
//calculate last day for every month
//status: working
int lastDay = 0;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 ||month == 12)
lastDay = lastDay + 31;
else
{
if (month == 4 || month == 6 || month == 9 || month == 11)
lastDay = lastDay + 30;
else
{ // Test for leap year(simple leap year)
if (year % 4 == 0)
lastDay = lastDay + 29;
else
lastDay = lastDay + 28;
}
}
return lastDay;
}
public static void displayHeader(int month)
{
//display the month headers in a 3x4 format
//status:working
switch (month)
{
case 1:
System.out.print(" January"); break;
case 2:
System.out.print(" February"); break;
case 3:
System.out.println(" March"); break;
case 4:
System.out.print(" April"); break;
case 5:
System.out.print(" May"); break;
case 6:
System.out.println(" June"); break;
case 7:
System.out.print(" July"); break;
case 8:
System.out.print(" August"); break;
case 9:
System.out.println(" September"); break;
case 10:
System.out.print(" October"); break;
case 11:
System.out.print(" November"); break;
case 12:
System.out.println(" December"); break;
}
}
public static int displayWeek(int month, int weekn, int lastDay, int day)
{
//status: almost working for every case. Only issue is the last week
//display the months one week at a time
int startDay = startDates(day, month);
int d = 0;
switch (weekn)
{
case 1:
{
for (int b = 1; b <= day; b++)
{
System.out.print(" ");
}
for (d=1;d<=7-day;d++)
{
System.out.print(" "+d);
}
if(month!=1)
day=7-(d%7);
break;
}
case 2:
{
for(d=8-day;d<=14-day;d++)
{
if(d<10)
{
System.out.print(" "+d);
}
else
{
System.out.print(" "+d);
}
}
break;
}
case 3:
{
for(d=15-day;d<=21-day;d++)
{
System.out.print(" "+d);
}
break;
}
case 4:
{
for(d=22-day;d<=28-day;d++)
{
System.out.print(" "+d);
}
break;
}
case 5:
{
for(d=29-day;d<=lastDay;d++)
{
System.out.print(" "+d);
}
if(lastDay%7==3)
System.out.print(" ");
if(lastDay%7==1)
System.out.print(" ");
break;
}
}
return d;
}
public static void main(String args[])
{
//declaration
int i, row, col, j, year, day, month;
String yearstr, daystr;
//input
yearstr = JOptionPane.showInputDialog("Enter a year: ");
year = Integer.parseInt(yearstr);
daystr = JOptionPane.showInputDialog("Enter a day for Jan.1: 0-Su, 1-M, 2-M, etc.");
day = Integer.parseInt(daystr);
System.out.println(" "+year);
//display month names
for(j=1;j<=3;j++)
{
displayHeader(j);
}
//week header
for(i=1;i<=3;i++)
{
System.out.print(" Su M Tu W Th F S");
System.out.print(" ");
}
System.out.println();
//display a row of 3 months
for(row=1;row<=5;row++)
{
for(col=1;col<=3;col++)
{
displayWeek(col,row,lastDayM(col,year),day);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
//2nd set of months
for(j=4;j<=6;j++)
{
displayHeader(j);
}
for(i=1;i<=3;i++)
{
System.out.print(" Su M Tu W Th F S");
System.out.print(" ");
}
System.out.println();
for(row=1;row<=5;row++)
{
for(col=4;col<=6;col++)
{
displayWeek(col,row,lastDayM(col,year),day);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
//3rd set of months
for(j=7;j<=9;j++)
{
displayHeader(j);
}
for(i=1;i<=3;i++)
{
System.out.print(" Su M Tu W Th F S");
System.out.print(" ");
}
System.out.println();
for(row=1;row<=5;row++)
{
for(col=7;col<=9;col++)
{
displayWeek(col,row,lastDayM(col,year),day);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
//last set
for(j=10;j<=12;j++)
{
displayHeader(j);
}
for(i=1;i<=3;i++)
{
System.out.print(" Su M Tu W Th F S");
System.out.print(" ");
}
System.out.println();
for(row=1;row<=5;row++)
{
for(col=10;col<=12;col++)
{
displayWeek(col,row,lastDayM(col,year),day);
System.out.print(" ");
}
System.out.println();
}
}
}
在你的代码,是1月1日或二月?另外,是星期一还是星期二? –
1是1月份,0是星期日,1是星期一等 – Asgore
我正在尝试阅读您的代码,但它很混乱。 'startDates()'存在,但它从来没有使用过。 。 。 –