两个while循环成一个

时间:2012-09-26 12:00:04

标签: java

我写了这段代码,提示用户输入他们的出生月份和出生年份。有一些错误检查:它们的月份必须是整数,也在1-12的范围内。同一年。就目前而言,我有两个while循环,我想不出一种方法将它们组合起来,当出现问题时仍然会收到错误消息。如果它只是说“输入错误:重新输入”,我可以想到它,但它需要说出输入有什么问题。它从月开始,那个月必须是好的才能继续询问这一年。如果这一年错了,那么它会要求重新进入年份 - 而不是一直回到月份。

如果有人有任何想法,我会很感激。用于检查它是否为整数的类是我需要使用的类。

while(!monthDone) {
           System.out.print("Enter Month of Birth: ");
           monthInput=scan.next();

           if(!(Type.isInteger(monthInput))) {
                  System.out.println("You need to enter an integer for month");
                  monthDone=false;
           } else {
                         MOB = Integer.valueOf(monthInput);

                         if (MOB<1 || MOB>12) {
                               System.out.println("Your month is out of range");
                               monthDone=false;
                         } else {
                               monthDone=true;
                         }

                         while(!yearDone) {
                                     System.out.print("Enter Year of Birth: ");
                                     yearInput=scan.next();
                                      if(!(Type.isInteger(yearInput))) {
                                             System.out.println("You need to enter an integer for year");
                                      }
                                      else {
                                             YOB = Integer.valueOf(yearInput);
                                             if (YOB<1912 || YOB>2012)  {
                                                    System.out.println("Your year is out of range");
                                                    yearDone=false;
                                                    }
                                                    else
                                                    yearDone=true;
                                             }
                                      }
                               }
                         }
         } // else closed
 } // Outer while close

我只是发布了while循环。当两个monthDone && yearDone == true时,我继续打印它们的年龄等等。

2 个答案:

答案 0 :(得分:2)

你可以简单地这样做:

while (!monthDone) {
...
}

while (!yearDone) {
...
}

因此,在您的月份输入正确完成之前,您不会进入年份输入。您会看到您可以轻松地将其扩展到日期等。

答案 1 :(得分:2)

这就是我如何在没有这么多筑巢的情况下解决这个问题。

Scanner scanner = new Scanner(System.in);
int month, year;
for (; ; scanner.nextLine()) {
    System.out.println("What is your month and year of birth as [1-12] [1900-2012]");
    if (!scanner.hasNextInt()) {
        System.out.println("Your month is not a number");
        continue;
    }
    month = scanner.nextInt();
    if (month < 1 || month > 12) {
        System.out.println("Your month is out of range");
        continue;
    }
    if (!scanner.hasNextInt()) {
        System.out.println("Your year is not a number");
        continue;
    }
    year = scanner.nextInt();
    if (year < 1900 || year > 2012) {
        System.out.println("Your year is out of range");
        continue;
    }
    break;
}
相关问题