do-while 循环中的 try-catch 块未正确循环并保留最后的输入结果
Try-catch blocks within do-while loops are not looping properly and carrying over the last input result
Scanner scan = new Scanner(System.in);
int year = -1;
int yearDuplicate = -1;
System.out.println("This calculator tells you whether a year that you "
+ "enter is a leap year.\nPlease enter a year:");
do{
try{
year = scan.nextInt();
}catch(Exception e){
System.out.println("ERROR. Please enter a numerical digit "
+ "without decimals:");
scan.next();
}
}while(year % 1 != 0);
do{
if(yearDuplicate % 4 == 0){
if(yearDuplicate % 100 == 0){
if(yearDuplicate % 400 == 0){
System.out.println(yearDuplicate + " is a leap year.");
}else{
System.out.println(yearDuplicate + " is not a leap year.");
}
}else{
System.out.println(yearDuplicate + " is a leap year.");
}
}else{
System.out.println(yearDuplicate + " is not a leap year.");
}
System.out.println("Enter another year or enter '-1' to quit:");
try{
yearDuplicate = scan.nextInt();
}catch(Exception e){
System.out.println("ERROR. Please enter a numerical digit "
+ "without decimals:");
scan.next();
}
}while(yearDuplicate != -1);
我正在尝试制作一个闰年计算器,它还会告诉您是否不小心输入了字符串或双精度而不是整数。我希望这个程序做的是 运行 “错误。请输入不带小数点的数字:”一遍又一遍,直到用户输入整数。但是,目前,对于第一个 try catch 块,如果我输入了两个错误的输入类型,它会给我这个错误消息:
test
ERROR. Please enter a numerical digit without decimals:
-1 is not a leap year.
Enter another year or enter '-1' to quit:
test
ERROR. Please enter a numerical digit without decimals:
BUILD SUCCESSFUL
第二个至少稍微好一点,它不断重复我输入的最后一个整数的结果(年份“是 a/not 闰年”)但至少它永远重复,不像第一个.
test
ERROR. Please enter a numerical digit without decimals:
10 is not a leap year.
Enter another year or enter '-1' to quit:
test
ERROR. Please enter a numerical digit without decimals:
10 is not a leap year.
Enter another year or enter '-1' to quit:
我只想重播错误消息,直到您输入正确的号码。如何摆脱先前的输入结果以及如何解决第一个 try catch 块仅 运行s 两次的事实?
我在网上看了看,但似乎如果我更改这个程序的一小部分,其他部分就会中断。如果我将第一个 while 循环的条件设置为 while(year % 1 == 0),它就会完全按照我的意愿执行;但这意味着如果它是一个整数,while 循环会重复,这与我想要它做的完全相反。如果我弄乱 scan.next();,它会给我一个无限的错误循环。
顺便说一下,有两个 try catch 块,因为用户在一年内有两次机会输入,还有两个地方我可能必须更正它们。如果我将它们都放在同一个 while 循环中,它会在第一次工作,但之后,每次我需要输入两个数字(一个用于第一次 try catch,它被第二次 try catch 覆盖)。
编辑:我将 year 和 yearDuplicate 的起始值更改为 0,在 try catch 块中声明了扫描器并删除了 scan.next();在两个 try catch 块中。现在,我正在使用第一个 try catch 块进行循环,但它仍会保留我之前的输入。
您的基本 'read until you get an integer' 块应该如下所示:
do {
try {
year = scan.nextInt();
if (year <= 0) { // see comment
System.out.println("ERROR. Please enter positive number");
}
} catch(Exception e){
System.out.println("ERROR. Please enter a number "
+ "without decimals");
scan.next();
year = -1;
}
} while (year <= 0);
因此,如果有 non-numeric 输入,您会主动设置一个会导致循环的值,而不是依赖可能已经存在的值。
此外,您要确保输入是正数。
鉴于你应该能够弄清楚整个程序。
我说“看评论”的地方,也许你可能想检查一个合理的年份;也许在大约 1752 年之前(当英国和美国采用公历时)扔掉任何东西。
您最初的问题可能是由于 year % 1 != 0
条件造成的。这意味着“年除以 1 的余数不为 0”。 non-negative 值始终如此。在这种情况下,我必须查看 Java 如何实现 %
负值才能回答。
修正了代码。
int year = 0;
int yearDuplicate = 0;
System.out.println("This calculator tells you whether a year that you "
+ "enter is a leap year.\nPlease enter a year:");
do{
Scanner scan = new Scanner(System.in);
try{
yearDuplicate = scan.nextInt();
if(yearDuplicate % 4 == 0){
if(yearDuplicate % 100 == 0){
if(yearDuplicate % 400 == 0){
System.out.println(yearDuplicate + " is a leap year.");
}else{
System.out.println(yearDuplicate + " is not a leap year.");
}
}else{
System.out.println(yearDuplicate + " is a leap year.");
}
}else{
System.out.println(yearDuplicate + " is not a leap year.");
}
System.out.println("Enter another year or enter '-1' to quit:");
}catch(Exception e){
System.out.println("ERROR. Please enter a numerical digit "
+ "without decimals:");
scan.next();
}
}while(yearDuplicate != -1);
基本上删除了第一个 try catch 块。使第二个 try catch 块包含整个 do while 循环。如果有人知道我以前犯过什么错误,为什么错了,我还是很想知道。
Scanner scan = new Scanner(System.in);
int year = -1;
int yearDuplicate = -1;
System.out.println("This calculator tells you whether a year that you "
+ "enter is a leap year.\nPlease enter a year:");
do{
try{
year = scan.nextInt();
}catch(Exception e){
System.out.println("ERROR. Please enter a numerical digit "
+ "without decimals:");
scan.next();
}
}while(year % 1 != 0);
do{
if(yearDuplicate % 4 == 0){
if(yearDuplicate % 100 == 0){
if(yearDuplicate % 400 == 0){
System.out.println(yearDuplicate + " is a leap year.");
}else{
System.out.println(yearDuplicate + " is not a leap year.");
}
}else{
System.out.println(yearDuplicate + " is a leap year.");
}
}else{
System.out.println(yearDuplicate + " is not a leap year.");
}
System.out.println("Enter another year or enter '-1' to quit:");
try{
yearDuplicate = scan.nextInt();
}catch(Exception e){
System.out.println("ERROR. Please enter a numerical digit "
+ "without decimals:");
scan.next();
}
}while(yearDuplicate != -1);
我正在尝试制作一个闰年计算器,它还会告诉您是否不小心输入了字符串或双精度而不是整数。我希望这个程序做的是 运行 “错误。请输入不带小数点的数字:”一遍又一遍,直到用户输入整数。但是,目前,对于第一个 try catch 块,如果我输入了两个错误的输入类型,它会给我这个错误消息:
test
ERROR. Please enter a numerical digit without decimals:
-1 is not a leap year.
Enter another year or enter '-1' to quit:
test
ERROR. Please enter a numerical digit without decimals:
BUILD SUCCESSFUL
第二个至少稍微好一点,它不断重复我输入的最后一个整数的结果(年份“是 a/not 闰年”)但至少它永远重复,不像第一个.
test
ERROR. Please enter a numerical digit without decimals:
10 is not a leap year.
Enter another year or enter '-1' to quit:
test
ERROR. Please enter a numerical digit without decimals:
10 is not a leap year.
Enter another year or enter '-1' to quit:
我只想重播错误消息,直到您输入正确的号码。如何摆脱先前的输入结果以及如何解决第一个 try catch 块仅 运行s 两次的事实?
我在网上看了看,但似乎如果我更改这个程序的一小部分,其他部分就会中断。如果我将第一个 while 循环的条件设置为 while(year % 1 == 0),它就会完全按照我的意愿执行;但这意味着如果它是一个整数,while 循环会重复,这与我想要它做的完全相反。如果我弄乱 scan.next();,它会给我一个无限的错误循环。
顺便说一下,有两个 try catch 块,因为用户在一年内有两次机会输入,还有两个地方我可能必须更正它们。如果我将它们都放在同一个 while 循环中,它会在第一次工作,但之后,每次我需要输入两个数字(一个用于第一次 try catch,它被第二次 try catch 覆盖)。
编辑:我将 year 和 yearDuplicate 的起始值更改为 0,在 try catch 块中声明了扫描器并删除了 scan.next();在两个 try catch 块中。现在,我正在使用第一个 try catch 块进行循环,但它仍会保留我之前的输入。
您的基本 'read until you get an integer' 块应该如下所示:
do {
try {
year = scan.nextInt();
if (year <= 0) { // see comment
System.out.println("ERROR. Please enter positive number");
}
} catch(Exception e){
System.out.println("ERROR. Please enter a number "
+ "without decimals");
scan.next();
year = -1;
}
} while (year <= 0);
因此,如果有 non-numeric 输入,您会主动设置一个会导致循环的值,而不是依赖可能已经存在的值。
此外,您要确保输入是正数。
鉴于你应该能够弄清楚整个程序。
我说“看评论”的地方,也许你可能想检查一个合理的年份;也许在大约 1752 年之前(当英国和美国采用公历时)扔掉任何东西。
您最初的问题可能是由于 year % 1 != 0
条件造成的。这意味着“年除以 1 的余数不为 0”。 non-negative 值始终如此。在这种情况下,我必须查看 Java 如何实现 %
负值才能回答。
修正了代码。
int year = 0;
int yearDuplicate = 0;
System.out.println("This calculator tells you whether a year that you "
+ "enter is a leap year.\nPlease enter a year:");
do{
Scanner scan = new Scanner(System.in);
try{
yearDuplicate = scan.nextInt();
if(yearDuplicate % 4 == 0){
if(yearDuplicate % 100 == 0){
if(yearDuplicate % 400 == 0){
System.out.println(yearDuplicate + " is a leap year.");
}else{
System.out.println(yearDuplicate + " is not a leap year.");
}
}else{
System.out.println(yearDuplicate + " is a leap year.");
}
}else{
System.out.println(yearDuplicate + " is not a leap year.");
}
System.out.println("Enter another year or enter '-1' to quit:");
}catch(Exception e){
System.out.println("ERROR. Please enter a numerical digit "
+ "without decimals:");
scan.next();
}
}while(yearDuplicate != -1);
基本上删除了第一个 try catch 块。使第二个 try catch 块包含整个 do while 循环。如果有人知道我以前犯过什么错误,为什么错了,我还是很想知道。