-2

我正在制作一个需要 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);
4

2 回答 2

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);
}
于 2013-07-26T19:41:43.507 回答
0

你的程序只是“停止”的原因是你只打印语句“请输入一个有效的月份......” ifi == 11并且你有你的for循环中断 if i >= 11。因此,这个条件永远不会满足。while 循环继续运行,即使此语句未打印。您可以在第一次尝试时输入非月份字符串,然后在第二次尝试时输入月份字符串,而您的 while 循环将被破坏。

以下是我如何改进您的代码以供本月使用。注意突出显示的细微变化。这些对于编写更好、更易读的代码很重要:

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 循环,检查有效输入。此外,Java 中的每个 if 都不需要 else。

于 2013-07-26T19:43:32.753 回答