Java 两个while循环成一个while循环
我编写了这段代码,提示用户输入他们的出生月份和出生年份。有一些错误检查:他们的月份必须是一个整数,并且在1-12范围内。今年也一样。目前,我有两个while循环,我想不出一种方法来组合它们,以便在出现错误时仍然能够得到错误消息。我可以想象,如果它只是说“输入错误:重新输入”,但它需要说输入有什么问题。从月份开始,这个月必须是好的,然后再问年份。如果年份不对,它会要求重新进入年份,而不是一直追溯到月份 如果有人有任何想法,我将不胜感激。用于检查它是否为整数的类是我必须使用的类Java 两个while循环成一个while循环,java,Java,我编写了这段代码,提示用户输入他们的出生月份和出生年份。有一些错误检查:他们的月份必须是一个整数,并且在1-12范围内。今年也一样。目前,我有两个while循环,我想不出一种方法来组合它们,以便在出现错误时仍然能够得到错误消息。我可以想象,如果它只是说“输入错误:重新输入”,但它需要说输入有什么问题。从月份开始,这个月必须是好的,然后再问年份。如果年份不对,它会要求重新进入年份,而不是一直追溯到月份 如果有人有任何想法,我将不胜感激。用于检查它是否为整数的类是我必须使用的类 while(!mon
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){
系统输出打印(“输入出生月份:”);
monthInput=scan.next();
如果(!(输入isInteger(monthInput))){
System.out.println(“您需要输入月份的整数”);
monthDone=假;
}否则{
MOB=整数。value of(monthInput);
如果(12){
System.out.println(“您的月份超出范围”);
monthDone=假;
}否则{
monthDone=true;
}
而(!yearDone){
系统输出打印(“输入出生年份:”);
yearInput=scan.next();
如果(!(键入isInteger(年份输入))){
System.out.println(“您需要为年份输入一个整数”);
}
否则{
YOB=整数。值(年输入);
如果(YOB2012){
System.out.println(“您的年份超出范围”);
yearDone=false;
}
其他的
yearDone=真;
}
}
}
}
}//否则关闭
}//关闭时外部
我只是发布while循环。当monthDone和yearDone都为true时,我会继续打印它们的年龄等。您可以简单地这样做:
while (!monthDone) {
...
}
while (!yearDone) {
...
}
因此,在正确完成月份输入之前,您不会进入年度输入。您将看到,您可以轻松地将其扩展到每月的某一天等。您可以简单地这样做:
while (!monthDone) {
...
}
while (!yearDone) {
...
}
因此,在正确完成月份输入之前,您不会进入年度输入。您将看到,您可以轻松地将其扩展到每月的某一天等。这就是我在没有太多嵌套的情况下如何布局它的方法
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;
}
Scanner Scanner=新的扫描仪(System.in);
int月,年;
对于(;;scanner.nextLine()){
System.out.println(“您出生的月份和年份是[1-12][1900-2012]”;
如果(!scanner.hasNextInt()){
System.out.println(“您的月份不是数字”);
继续;
}
月份=scanner.nextInt();
如果(月<1 | |月>12){
System.out.println(“您的月份超出范围”);
继续;
}
如果(!scanner.hasNextInt()){
System.out.println(“您的年份不是一个数字”);
继续;
}
年份=scanner.nextInt();
如果(年份<1900年|年>2012年){
System.out.println(“您的年份超出范围”);
继续;
}
打破
}
这就是我在没有太多嵌套的情况下如何布局它的方法
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;
}
Scanner Scanner=新的扫描仪(System.in);
int月,年;
对于(;;scanner.nextLine()){
System.out.println(“您出生的月份和年份是[1-12][1900-2012]”;
如果(!scanner.hasNextInt()){
System.out.println(“您的月份不是数字”);
继续;
}
月份=scanner.nextInt();
如果(月<1 | |月>12){
System.out.println(“您的月份超出范围”);
继续;
}
如果(!scanner.hasNextInt()){
System.out.println(“您的年份不是一个数字”);
继续;
}
年份=scanner.nextInt();
如果(年份<1900年|年>2012年){
System.out.println(“您的年份超出范围”);
继续;
}
打破
}
为什么要合并它们?目前,代码至少在概念上很容易理解。在这种情况下,仅仅通过一些技巧来减少代码似乎不是一个好主意。与其嵌套两个循环,不如将它们放在彼此之后。首先是几个月的while循环,然后是几年的while循环。我会要求将月份和年份作为一个输入。这将简化大部分代码。顺便说一句:如果我已经100多岁了怎么办?这不是对你问题的回答,但你真的应该把这个代码分成几个不同的方法-缩进是疯狂的。我被告知它可以在一个循环中完成,只需要一个标志。我只是想知道怎么做。你为什么要合并它们?目前,代码至少在概念上很容易理解。在这种情况下,仅仅通过一些技巧来减少代码似乎不是一个好主意。与其嵌套两个循环,不如将它们放在彼此之后。首先是几个月的while循环,然后是几年的while循环。我会要求将月份和年份作为一个输入。这将简化大部分代码。顺便说一句:如果我已经100多岁了怎么办?这不是对你问题的回答,但你真的应该把这个代码分成几个不同的方法-缩进是疯狂的。我被告知它可以在一个循环中完成,只需要一个标志。我是jus