我正在尝试将用户输入限制为1到100,000之间的数字。我做了一个try / catch异常,但它继续标记尝试并抓住说一个人需要另一个,当我同时拥有它们时?
System.out.println("Cuenta con abono?");
String abono = scan.nextLine();
if (abono.equals("si") || abono.equals("Si"))
{
do
{
try {
System.out.println("Cual es el numero de su abono? "); //la key para el hashmap que en este caso se asume que el numero de abono es el numero de asiento en el estadio
Integer numId = Integer.parseInt(scan.nextLine());
if (numId >= 1 && numId <= 100000) {
}
else {
System.out.println("Numero de abono invalido");
scan.nextLine();
}
catch (InputMismatchException error) {
System.out.println("Trate de nuevo (Debe de ser de uno a cien mil)");
scan.nextLine();
}
}
System.out.println("Nombre: "); //el valor en si (nombre)
String nombre = scan.nextLine();
String oldVal = hm.put(numId, nombre);
if (oldVal!=null) {
System.out.println("Numero ID :" + numId + " es "
+ oldVal + " y ha sido sobreescrito por " + nombre);
}
else { //este else termina si no tiene abono
break;
}
答案 0 :(得分:2)
如果你打扰你的代码,你会看到:
try {
^---#1open
[..snip..]
if (numId >= 1 && numId <= 100000) {
^---#2open
}
^---#2close
else {
^---#3open
System.out.println("Numero de abono invalido");
scan.nextLine();
}
^--#3close
catch (InputMismatchException error) {'
^---catch without try, because you're still inside brace #1
答案 1 :(得分:0)
您缺少在以下位置关闭大括号:
if
else
catch
之后的第二个右大括号应位于代码块的末尾,沿着while
结束循环。