public class calendar
{
public static void printMonth(int numDays, int startDays)
{
System.out.println("Su Mo Tu We Th Fr Sa");
for(int j=0; startDays >= j; j++)
{
if (j==0)
System.out.print(" ");
else
System.out.print(" ");
}
for (int i=1; numDays >= i; i++)
{
if (i<=7-startDays)
System.out.print(i + " ");
else if (i<10)
System.out.print(" " + i + " ");
else
System.out.print(i + " ");
if ((i == 7-startDays) || (i % 7+(startDays-7) == 0))
System.out.print("\n");
}
}
public static void main(String args[])
{
printMonth(28,5);
}
}
我用 java 写了这个来打印一个 2d 日历的东西,我可以说它是粗制滥造的,并且对更有效的替代方案感到好奇。