代码由于某种原因不起作用

时间:2013-07-26 19:24:31

标签: java for-loop while-loop

我正在制作一个需要3个输入的东西,如“1500,1和1”或“1500,1月和1”并返回“1月1日,1500”或“1/1/1500”,我遇到了一些问题在当天的一部分,但有人已经告诉我如何解决它,现在我有月份部分的问题,我有点快,我还没弄清楚为什么它不工作,它应该看看是否输入是一个有效月份,如果是,那么它输出月份(这部分仅用于测试),如果不是那么它应该说“请使用有效月份或1到12之间的数字”,但是我写的任何不是一个月它停止的东西,并没有输出任何东西,即使我放了一个月后它只是没有做任何事情,我试图看看是否有任何错误,但我没有找到任何,这是我使用的代码:

   Scanner scan = new Scanner(System.in);
   String mx;
   System.out.println("Insert Month");
   String[] mm = {"january","february","march","april","may","june","july","august","september","october","november","december"};
   int mz = 0;
   while (0 < 1){
   mx = scan.nextLine();
       mx = mx.toLowerCase();
       for(int i = 0; i < 11; i++){
           if (mx.equals(mm[i])){
               mz = i + 1;
               break;
           }
           else {
               if(i == 11){
                   System.out.println("please use a valid month or a number between 1 and 12");
               }
               else{
               }
           }
       } 
   //}
   if(mz > 0){
       break;
   }
   else {}
   }
   System.out.println(mx);

2 个答案:

答案 0 :(得分:0)

您没有使用有意义的变量名,使您的代码有点难以阅读和维护。所以,我必须从头开始创建以下代码:

public static void main(String[] args)
{
        Scanner keyboard = new Scanner(System.in);

        String month = getMonthName(getInt("Enter Month: ", keyboard) - 1);
        int day = getInt("Enter Day: ", keyboard);
        int year = getInt("Enter Year: ", keyboard);

        System.out.printf("%s %d, %d\n", month, day, year);

}

public static String getMonthName(final int monthNo)
{
       String[] months = {"january","february","march","april","may","june","july","august","september","october","november","december"};
       return months[monthNo];
} 


public static int getInt(final String msg, Scanner keyboard)
{
        System.out.print(msg);
        return keyboard.nextInt();
}

上面的代码不会执行和输入验证,因为您可能已经注意到了。例如,如果要验证月份输入,则if条件可能如下所示:

if (month < 0 || month < 12)
{
 System.out.println("Invalid month number entered");      
 System.exit(0);
}

答案 1 :(得分:0)

您的程序“停止”的原因是您只打印语句“请输入有效的月份...”i == 11如果for您的i >= 11循环中断Scanner scan = new Scanner(System.in); //initialize to empty string String mx = ""; System.out.println("Insert Month"); //use good naming conventions for easier code readability String[] validMonths = {"january","february","march","april","may","june","july","august","september","october","november","december"}; //using a boolean to break makes much more sense than the way you have written it with an infinite loop and a manual break statement boolean noMonth = true; while (noMonth){ mx = scan.nextLine(); for(int i = 0; i < 12; i++){ //rather than convert to lowercase, use this handy String method //also compares for valid number entries if (mx.equalsIgnoreCase(validMonths[i]) || mx.equals(Integer.toString(i+1))){ noMonth = false; break; } } if(noMonth){ System.out.println("please use a valid month or a number between 1 and 12"); } } System.out.println(mx); }。因此,这种情况永远不会得到满足。 while循环继续运行,即使此语句未打印。您可以在第一次尝试时输入非月份字符串,然后在第二次尝试时输入一个月份字符串,并且您的while循环将被破坏。

以下是我改进您的代码以便在本月工作的方法。注意突出显示的细微变化。这些对于编写更好,更易读的代码非常重要:

{{1}}

创建一个新的while循环以获取当天,并在之后的一年中创建一个新的循环,检查有效输入。此外,每个if都不需要Java中的else。