我想问几个问题,请参阅我在代码中添加的评论部分,谢谢。
package test;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
/* Task:
prompt user to read two integers and display the sum. prompt user to read the number again if the input is incorrect */
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean accept_a = false;
boolean accept_b = false;
int a;
int b;
while (accept_a == false) {
try {
System.out.print("Input A: ");
a = input.nextInt(); /* 1. Let's enter "abc" to trigger the exception handling part first*/
accept_a = true;
} catch (InputMismatchException ex) {
System.out.println("Input is Wrong");
input.nextLine(); /* 2. I am still not familiar with nextLine() parameter after reading the java manual, would you mind to explain more? All I want to do is "Clear Scanner Buffer" so it wont loop for the println and ask user to input A again, is it a correct way to do it? */
}
}
while (accept_b == false) {
try {
System.out.print("Input B: ");
b = input.nextInt();
accept_b = true;
} catch (InputMismatchException ex) { /*3. Since this is similar to the above situation, is it possible to reuse the try-catch block to handling b (or even more input like c d e...) exception? */
System.out.println("Input is Wrong");
input.nextLine();
}
}
System.out.println("The sum is " + (a + b)); /* 4. Why a & b is not found?*/
}
}
答案 0 :(得分:1)
- 阅读java手册后我还不熟悉nextLine()参数,你介意解释一下吗?我想做的就是" Clear Scanner Buffer"因此它不会为println循环并要求用户再次输入A,这是一种正确的方法吗?
醇>
在input.nextLine();
之后使用input.nextInt();
是为了清除输入流中的剩余内容,因为(至少)新行字符仍在缓冲区中,将内容保留在缓冲区中如果input.nextInt();
没有先清除<{1}},会导致Exception
继续投放
- 由于这与上述情况相似,是否可以重用try-catch块来处理b(甚至更多的输入,如c d e ...)异常?
醇>
你可以,但如果输入b错误会怎样?你要求用户重新输入输入吗?如果您有100个输入并且它们最后一个输入错误会发生什么?您实际上最好还是编写一个方法来执行此操作,即提示用户输入值并返回该值的方法
例如......
public int promptForIntValue(String prompt) {
int value = -1;
boolean accepted = false;
do {
try {
System.out.print(prompt);
value = input.nextInt();
accepted = true;
} catch (InputMismatchException ex) {
System.out.println("Input is Wrong");
input.nextLine();
}
} while (!accepted);
return value;
}
- 为何选择&amp; b找不到?
醇>
因为它们尚未初始化且编译器无法确定它们是否具有有效值...
尝试更改它的更多内容。
int a = 0;
int b = 0;
答案 1 :(得分:1)
让我们简化并提取一种方法,
private static int readInt(String name, Scanner input) {
while (true) {
try {
System.out.printf("Input %s: ", name);
return input.nextInt();
} catch (InputMismatchException ex) {
System.out.printf("Input %s is Wrong%n", input.nextLine());
}
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = readInt("A", input);
int b = readInt("B", input);
System.out.println("The sum is " + (a + b));
}
答案 2 :(得分:0)
我已对该问题进行评论。
package test;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean accept_a = false;
boolean accept_b = false;
int a=0;
int b=0;
System.out.print("Input A: ");
while (accept_a == false) {
try {
a = input.nextInt(); // it looks for integer token otherwise exception
accept_a = true;
} catch (InputMismatchException ex) {
System.out.println("Input is Wrong");
input.next(); // Move to next other wise exception // you can use hasNextInt()
}
}
System.out.print("Input B: ");
while (accept_b == false) {
try {
b = input.nextInt();
accept_b = true;
} catch (InputMismatchException ex) {
System.out.println("Input is Wrong");
input.next();
}
}
System.out.println("The sum is " + (a + b)); // complier doesn't know wheather they have initialised or not because of try-catch blocks. so explicitly initialised them.
}
}
答案 3 :(得分:-1)