(初学者)Java程序跳过代码行

时间:2017-11-09 02:34:14

标签: java

我目前正在为学校制定日期差异计划,我无法弄清楚为什么它会完全跳过“secondMonth”输入。它打印出最后一行,但只接受“secondYear”的输入并打破程序。我在Eclipse中编写了程序,我在Eclipse中编译它。非常感谢任何帮助。

我的代码:

if (secondMonth.equalsIgnoreCase("janurary"))
{
    secondMonthNumber = 1;
}
else if (secondMonth.equalsIgnoreCase("february"))
{
    secondMonthNumber = 2;
}
else if (secondMonth.equalsIgnoreCase("march"))
{
    secondMonthNumber = 3;
}
else if (secondMonth.equalsIgnoreCase("april"))
{
    secondMonthNumber = 4;
}
else if (secondMonth.equalsIgnoreCase("may"))
{
    secondMonthNumber = 5;
}
else if (secondMonth.equalsIgnoreCase("june"))
{
    secondMonthNumber = 6;
}
else if (secondMonth.equalsIgnoreCase("july"))
{
    secondMonthNumber = 7;
}
else if (secondMonth.equalsIgnoreCase("august"))
{
    secondMonthNumber = 8;
}
else if (secondMonth.equalsIgnoreCase("september"))
{
    secondMonthNumber = 9;
}
else if (secondMonth.equalsIgnoreCase("october"))
{
    secondMonthNumber = 10;
}
else if (secondMonth.equalsIgnoreCase("november"))
{
    secondMonthNumber = 11;
}
else
{
    secondMonthNumber = 12;
}


System.out.print("Enter year: ");
secondYear = scan.nextInt();


System.out.print("These dates are " + (firstYear - secondYear) + "years and " + (firstMonthNumber - secondMonthNumber) + "months apart.");

1 个答案:

答案 0 :(得分:-3)

您是否尝试在代码中添加循环?你可以让它永远继续(循环),或者你可以编写一个for循环(它会在设定的时间内进行)。

public static void main(String args[]){

   while(true){

   /*enter code in here *\

   }

}

您可以制作一个Arraylist,以便在扫描用户输入时保存用户获得的信息。

...或

你可以添加一个for循环(只能工作x次(在例子中命名的时间))。

//declare pvsm up here
//create a stationary array
int[] dates = new int[times];
for(int x  = 0; x < times; x++){

   dates[x] = /*information you are trying to load *\
}