所以这是我的防撞扫描程序类的代码:
import java.util.*;
public class BPScanner {
Scanner kb = new Scanner(System.in);
public int nextInt() {
while (true) {
try {
String input = kb.nextLine();
int i = Integer.parseInt(input);
return i;
}
catch (NumberFormatException e1) {}
catch (NoSuchElementException e2) {}
System.out.print("\nPlease input an integer: ");
kb.close();
kb = new Scanner(System.in);
}
}
}
我从另一个班级打电话给这个班级:
public void scnr() {
while (true){
System.out.print("Type a num (for test), (0 to break)");
int n = bpkb.nextInt();
if (n == 0) break;
System.out.println(n);
}
}
当我运行它时,它返回一个无限循环,一直说:
Please input an integer:
Please input an integer:
Please input an integer:
Please input an integer:
任何想法如何解决? 非常感谢你提前。
答案 0 :(得分:1)
只需从nextInt
功能中删除以下代码:
kb.close();
kb = new Scanner(System.in);