我的公历显示为阶梯状,并且每行重复1次。我做错了什么?
package unit8_1gregoriancalendar;
import java.util.GregorianCalendar;
@SuppressWarnings("serial") // This will suppress problems with the class
public class Lab8_1GregorianCalendar extends GregorianCalendar
{
// Create a Gregorian calendar type
static GregorianCalendar gregorianObject = new GregorianCalendar();
public static void main(String[] args)
{
// This gets the current month, but need to add one for January = 0 in the GregorianCalendar
int currentMonth = gregorianObject.get(Lab8_1GregorianCalendar.MONTH)+1;
int currentYear = gregorianObject.get(Lab8_1GregorianCalendar.YEAR);
printOutMonth(currentYear, currentMonth);
} // This ends Main() Method
static void printOutMonth(int year, int month)
{
int startDay = gregorianObject.get(Lab8_1GregorianCalendar.DAY_OF_WEEK);
int monthDaysEnd = howManyDays(month,year);
//This Displays the Calendar Header
String monthName = getMonthName();
System.out.println();
System.out.println(" " + monthName + " " + year);
System.out.println("----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
fillTheCells(startDay, monthDaysEnd);
} // This ends printOutMonth Method
static int howManyDays(int month, int year)
{
switch(month)
{
case 1: return 31;
case 2:{ if (gregorianObject.isLeapYear(year))
return 29;
else
return 28;
}
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11: return 30;
case 12: return 31;
default: return 30;
} // month switch case
} // this ends howManyDays method
static String getMonthName()
{
int monthIndicator = gregorianObject.get(Lab8_1GregorianCalendar.MONTH);
switch (monthIndicator)
{
case 0: return "January";
case 1:return "February";
case 2: return "March";
case 3: return "April";
case 4: return "May";
case 5: return "June";
case 6: return "July";
case 7: return "August";
case 8: return "September";
case 9: return "October";
case 10: return "November";
case 11: return "December";
default: return "Month";
} // This ends monthIndicator Switch Structure
} // This ends getMonthName
static void fillTheCells(int intStartDay, int innerMonthDaysEnd)
{ //The dayCellCount loop = 7 days per week
// Tells it to print what fits on that week AND only til end of month
int dayCellCount=1;
for(int weekCellCount=1; weekCellCount <= 6; weekCellCount++)
{
if(weekCellCount==1);
dayCellCount=1;
while((dayCellCount<=weekCellCount*7)&&((dayCellCount-intStartDay)<=innerMonthDaysEnd))
{
//Print 4 blanks per day until you get to where the month begins
if(dayCellCount<=intStartDay)
{
System.out.print(" ");
}
// This will Print 3 blanks and the date if date has just 1 digit
else if(dayCellCount-intStartDay<10)
{
System.out.print(" "+(dayCellCount-intStartDay));
}
// This will Print 2 blanks and the date if the date has 2 digits until the end of month
else if(dayCellCount-intStartDay <= innerMonthDaysEnd)
{
System.out.print(" " +(dayCellCount-intStartDay));
}
// This will get the dayCellCount to step from day to day
dayCellCount++;
} // This ends the dayCellCount
System.out.println();
} // This ends the weekCellCount loop
intStartDay=(intStartDay+innerMonthDaysEnd)%7;
} // This ends fillTheCells() Method
} // This ends the Lab8_1GregorianCalendar Class
答案 0 :(得分:0)
if(weekCellCount==1); <-----
在if语句的末尾有一个分号,因此它被视为空白语句,并且每周都会运行设置dayCellCount的以下行。