在Java中检测到异常时,如何重复代码?

在Java中检测到异常时,如何重复代码?,java,eclipse,loops,exception,user-input,Java,Eclipse,Loops,Exception,User Input,我有一个“ScanInput”类: 公共最终类扫描输入{ 公共字符串输出; 公共国际输出; 公共输入(){ } 公共最终无效扫描(字符串文本){ 系统输出打印(文本); @抑制警告(“资源”) Scanner inputI=新扫描仪(System.in); int输入; while(true){ 试一试{ input=inputI.nextInt(); 打破 }捕获(例外e){ Scanit(文本); } } 输出=输入; } } 被这个类称为“哇”的(这个词对吗?我是java新手): 公共类

我有一个“ScanInput”类:

公共最终类扫描输入{
公共字符串输出;
公共国际输出;
公共输入(){
}
公共最终无效扫描(字符串文本){
系统输出打印(文本);
@抑制警告(“资源”)
Scanner inputI=新扫描仪(System.in);
int输入;
while(true){
试一试{
input=inputI.nextInt();
打破
}捕获(例外e){
Scanit(文本);
}
}
输出=输入;
}
}
被这个类称为“哇”的(这个词对吗?我是java新手):

公共类哇{
公共静态void main(字符串[]args){
ScanInput编号=新的ScanInput();
数字.scanit(“输入数字:”);
System.out.println(“您输入:“+number.outputI”);
}
}

当一个整数没有被输入时,我试图让它自己重复。如果我第一次键入一个整数,它将工作并打印您输入的“+numbers.outputI”。如果我不这样做,它会再次“输入数字:”但它会陷入一个循环,只会打印您输入的“+NUMBERS.outputI”)不考虑输入。如果第一次不是整数,如何使其工作?

scanit
中调用
scanit
是递归,递归是循环的一种方式


您还有一个
while(true)
块;另一个循环。这是两个循环。一个循环太多了。移除其中一个,您就可以使其正常工作。任何一个;您的选择。

scanit
中调用
scanit
是递归,递归是循环的一种方式


您还有一个
while(true)
块;另一个循环。这是两个循环。一个循环太多了。移除其中一个,您就可以使其正常工作。任何一个;您的选择。

以下代码将解决您的问题。关键是您应该使用
inputI.next()
清除缓冲区中的非数字字符串。否则,
inputI.nextInt()

public final void scanInt(String text) {
    @SuppressWarnings("resource")
    Scanner inputI = new Scanner(System.in);
    int input;

    while(true) {
        try {
            System.out.print(text);
            input = inputI.nextInt();
            break;
        } catch (Exception e) {
            inputI.next();  // read the token does not match the Integer in buffer
        }
    }
    outputI = input;
}

下面的代码将解决您的问题。关键是您应该使用
inputI.next()
清除缓冲区中的非数字字符串。否则,
inputI.nextInt()

public final void scanInt(String text) {
    @SuppressWarnings("resource")
    Scanner inputI = new Scanner(System.in);
    int input;

    while(true) {
        try {
            System.out.print(text);
            input = inputI.nextInt();
            break;
        } catch (Exception e) {
            inputI.next();  // read the token does not match the Integer in buffer
        }
    }
    outputI = input;
}

只是另一种选择。无需尝试/捕获:

public int outputI;
private Scanner inputI = new Scanner(System.in);

public final void scanInt(String text) {
    String num = "";
    while(num.equals("")) {
        System.out.print(text);
        num = inputI.nextLine();
        // Is the entry a numerical string?
        if (!num.matches("\\d+")) {
            // No...
            System.out.println("Invalid Entry! Integer Number Only!");
            num = "";  // null string num so as to ensure another loop.
        }

    }
    // Convert string numerical value entry to Integer.
    this.outputI = Integer.parseInt(num);
}

只是另一种选择。无需尝试/捕获:

public int outputI;
private Scanner inputI = new Scanner(System.in);

public final void scanInt(String text) {
    String num = "";
    while(num.equals("")) {
        System.out.print(text);
        num = inputI.nextLine();
        // Is the entry a numerical string?
        if (!num.matches("\\d+")) {
            // No...
            System.out.println("Invalid Entry! Integer Number Only!");
            num = "";  // null string num so as to ensure another loop.
        }

    }
    // Convert string numerical value entry to Integer.
    this.outputI = Integer.parseInt(num);
}

您当前的代码有什么问题?(它是做什么的,你期待什么不一样的事情吗)@因为我真的不知道到底发生了什么。当它捕获到异常时,它会循环回到该方法中,但我猜它不会退出该方法。我希望它在输入intiger后继续发送到System.out.println(“您输入:“+numbers.outputI”)。您当前的代码有什么问题?(它是做什么的,你期待什么不一样的事情吗)@因为我真的不知道到底发生了什么。当它捕获到异常时,它会循环回到该方法中,但我猜它不会退出该方法。我希望在输入intiger后,它能继续发送到System.out.println(“您输入:“+numbers.outputI”)。