所以我正在尝试为我的计算机编程课程编写程序,它应该打印出本月的日历。我希望我的程序在一个月的第一天拿出来而不要求那个人开始这一天,所以我找到了一个方程式,它会这样做,并尝试在程序中使用它。它编译得很好,但是当它到达应该开始等式的部分并打印出日历时,程序就......停止了。它完全停止运行并返回到V:\ JavaPrograms>命令提示符。
我无法弄清楚为什么会这样。这是代码:
import java.util.*;
public class Lesson13p2_ThuotteEmily
{
public static void main(String args[])
{
Scanner r=new Scanner(System.in);
System.out.println("What year is it? ");
int y=r.nextInt();
System.out.println("Please pick a month from the list below:");
System.out.println("\n1. January\n2. February\n3. March");
Scanner b=new Scanner(System.in);
System.out.println("Enter your choice here by inputting the number next to the month: ");
int m=b.nextInt();
int d=1, o;
switch(m)
{
case 1:
o=13;
int n=d+2*o+(3*(o+1)/5)+y+(y/4)-(y/100)+(y/400)+2;
if(n==1)
{
System.out.println("Here is your calendar for January, "+y+", which starts on a Sunday.");
System.out.println("\t\tJanuary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t");
}
System.out.println("");
}
++j;
}
else if(n==2)
{
System.out.println("Here is your calendar for January, "+y+", which starts on a Monday.");
System.out.println("\t\tJanuary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t");
}
System.out.println("");
}
++j;
}
else if(n==3)
{
System.out.println("Here is your calendar for January, "+y+", which starts on a Tuesday.");
System.out.println("\t\tJanuary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==4)
{
System.out.println("Here is your calendar for January, "+y+", which starts on a Wednesday.");
System.out.println("\t\tJanuary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==5)
{
System.out.println("Here is your calendar for January, "+y+", which starts on a Thursday.");
System.out.println("\t\tJanuary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==6)
{
System.out.println("Here is your calendar for January, "+y+", which starts on a Friday.");
System.out.println("\t\tJanuary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==7)
{
System.out.println("Here is your calendar for January, "+y+", which starts on a Saturday.");
System.out.println("\t\tJanuary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
case 2:
if((y%100!=0||y%400==0)&&y%4==0)
{
o=14;
int n=d+2*o+(3*(o+1)/5)+y+(y/4)-(y/100)+(y/400)+2;
if(n==1)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Sunday and is a leap year.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=29;k++)
{
System.out.print((++j)+"\t");
}
System.out.println("");
}
++j;
}
else if(n==2)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Monday and is a leap year.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=29;k++)
{
System.out.print((++j)+"\t\t");
}
System.out.println("");
}
++j;
}
else if(n==3)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Tuesday and is a leap year.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=29;k++)
{
System.out.print((++j)+"\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==4)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Wednesday and is a leap year.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=29;k++)
{
System.out.print((++j)+"\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==5)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Thursday and is a leap year.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=29;k++)
{
System.out.print((++j)+"\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==6)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Friday and is a leap year.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=29;k++)
{
System.out.print((++j)+"\t\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==7)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Saturday and is a leap year.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=29;k++)
{
System.out.print((++j)+"\t\t\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
}
else
{
o=14;
int n=d+2*o+(3*(o+1)/5)+y+(y/4)-(y/100)+(y/400)+2;
if(n==1)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Sunday.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=28;k++)
{
System.out.print((++j)+"\t");
}
System.out.println("");
}
++j;
}
else if(n==2)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Monday.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=28;k++)
{
System.out.print((++j)+"\t\t");
}
System.out.println("");
}
++j;
}
else if(n==3)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Tuesday.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=28;k++)
{
System.out.print((++j)+"\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==4)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Wednesday.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=28;k++)
{
System.out.print((++j)+"\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==5)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Thursday.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=28;k++)
{
System.out.print((++j)+"\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==6)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Friday.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=28;k++)
{
System.out.print((++j)+"\t\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==7)
{
System.out.println("Here is your calendar for February, "+y+", which starts on a Saturday.");
System.out.println("\t\tFebruary\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=28;k++)
{
System.out.print((++j)+"\t\t\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
}
case 3:
o=3;
int n=d+2*o+(3*(o+1)/5)+y+(y/4)-(y/100)+(y/400)+2;
if(n==1)
{
System.out.println("Here is your calendar for March, "+y+", which starts on a Sunday.");
System.out.println("\t\tMarch\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t");
}
System.out.println("");
}
++j;
}
else if(n==2)
{
System.out.println("Here is your calendar for March, "+y+", which starts on a Monday.");
System.out.println("\t\tMarch\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t");
}
System.out.println("");
}
++j;
}
else if(n==3)
{
System.out.println("Here is your calendar for March, "+y+", which starts on a Tuesday.");
System.out.println("\t\tMarch\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==4)
{
System.out.println("Here is your calendar for March, "+y+", which starts on a Wednesday.");
System.out.println("\t\tMarch\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==5)
{
System.out.println("Here is your calendar for March, "+y+", which starts on a Thursday.");
System.out.println("\t\tMarch\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==6)
{
System.out.println("Here is your calendar for March, "+y+", which starts on a Friday.");
System.out.println("\t\tMarch\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
else if(n==7)
{
System.out.println("Here is your calendar for March, "+y+", which starts on a Saturday.");
System.out.println("\t\tMarch\nSun\tMon\tTues\tWed\tThurs\tFri\tSat\n------------------------------------------------------------------------------");
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t\t\t\t\t");
}
System.out.println("");
}
++j;
}
}
}
}
另外,我首先尝试使用常规if语句,但它没有用。
答案 0 :(得分:1)
你的n等式是不正确的
n=d+2*o+(3*(o+1)/5)+y+(y/4)-(y/100)+(y/400)+2;
2009年的一年将给出至少2009年的n。事实上,2009年1月的n更像是2533.你只有1到7之间的n条件,所以2533的n并不令人惊讶做任何事。我怀疑你可能想要
n=(d+2*o+(3*(o+1)/5)+y+(y/4)-(y/100)+(y/400)+2)%7;
可能最后使用+1,因为您使用的是1-7天而不是0-6
您似乎一遍又一遍地重复相同的代码。这些应该被提取到单独的函数中并在需要时调用。这将使您的代码更易于阅读和更易于维护(以及更短的代码。
例如,以下内容重复了很多次!
int j = 0, k=0;
while(j<=4)
{
for(k=1;k<=31;k++)
{
System.out.print((++j)+"\t\t\t");
}
System.out.println("");
}
++j;
答案 1 :(得分:-1)
可能正在发生的事情是你没有从命令行(System.in)正确读取值,从而有一个int,它不会在开关中给出预期的值,只是绕过你所有的日历逻辑。确保你正在读取正确的值。如果你没有使用调试器,你可以测试它的方法是硬编码一个预期的整数值,如整数“1”并使用该值代替你的那个值从命令行读入。程序返回命令提示符这一事实表明程序是从main返回而不是停止。如果程序挂在无限循环中,例如它会给出挂起的外观,但你永远不会回到命令提示符。希望这会有所帮助。