首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java中以递归方式捕获异常

在Java中以递归方式捕获异常
EN

Stack Overflow用户
提问于 2014-01-18 23:00:54
回答 1查看 610关注 0票数 3

我目前正在开发一个小游戏,我遇到了一个无法修复的问题。我有一个读取输入并将其存储在变量中的方法。对于任何错误的输入,将抛出一个IllegalArgumentException,您可以在此再次尝试输入。但是如果你再一次做错了,它就会进入下一个输入类型。但我希望它要求输入,直到输入有效。我的导师告诉我用try and catch来做,我也这样做了,但正如我所说的,它只会做两次,然后继续。

下面是代码:

代码语言:javascript
复制
 public void readSettings(){
    Scanner userinput = new Scanner(System.in);
    System.out.println("Wie lange soll der Code sein? (4-10):");
    String input = userinput.nextLine();
    //Eingabe der Länge des zu lösenden Codes. Bei einer Eingabe außerhab des Wertebereichs wird dies gemeldet und neu gefragt.
    try{
        if (input.matches("[4-9]|10")) {
            this.codelength = Integer.parseInt(input);  
        }
        else {
            throw new IllegalArgumentException();
        }
    }

    catch (IllegalArgumentException e) {
        System.out.println("Eingabe außerhalb des Wertebreichs!"); 
        //readSettings();
        System.out.println("Wie lange soll der Code sein? (4-10):");
        input = userinput.nextLine();
        if (input.matches("[4-9]|10")) {
            this.codelength = Integer.parseInt(input);  
        }
        else {
            throw new IllegalArgumentException(e);
        }
    } finally {

        System.out.println("Welche Ziffern sind erlaubt? 0- (1-9):");
        input = userinput.nextLine();
        //Hier wird die valuerange(Also die Maximale Zahl in der Reihe) abgefragt. 
        //Auch hier wird eine falche Eingabe abgefangen und der Input neu gestartet. (Leider nicht sehr elegant und benutzerfreundlich.
        try {
            if (input.matches("[1-9]")) {
                this.valuerange = Integer.parseInt(input);  
            }
            else {
                throw new IllegalArgumentException();
            }
        }
        catch (IllegalArgumentException f) {
            System.out.println("Eingabe außerhalb des Wertebreichs!"); 
            //readSettings();
            System.out.println("Welche Ziffern sind erlaubt? 0- (1-9):");
            input = userinput.nextLine();
            if (input.matches("[1-9]")) {
                this.valuerange = Integer.parseInt(input);  
            }
            else {
                throw new IllegalArgumentException(f);
            }
        } finally {

            //Falls der Modus nicht Cpmouter gegen Computer ist, wird ein Spielername mit abgefragt.
            try {
                if(!cpumode) {
                    System.out.println("Spielername:");
                    this.spielername = userinput.nextLine();
                    //Falls kein input bein Namen vorhanden, wird ein Fehler ausgegeben.
                    if (spielername.length()==0) {
                        throw new IllegalArgumentException("Fehler, kein Spielername eingegeben!" );  

                    }     
                }
            } catch (IllegalArgumentException e) {
                System.out.println("Spielername:");
                this.spielername = userinput.nextLine();
                if (spielername.length()==0) {
                    //throw new IllegalArgumentException("Fehler, kein Spielername eingegeben!" );  
                    throw new IllegalArgumentException(e);
                }     

            }   
        }
    }
}       

我希望你能帮助我。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2014-01-18 23:33:15

第一步是意识到你是在重复你的“读代码长度”。这通常意味着两件事: 1)您应该将代码提取到一个方法中并多次调用该方法,或者2)您应该使用循环并在循环中编写代码。(或两者都有)

循环的建议应该让人耳目一新,因为只有这样(或递归...)允许您根据需要重复该操作不固定的次数。在伪代码中:

代码语言:javascript
复制
repeat
    readNumber
until number is valid

如您所见,条件位于循环的底部。在Java语言中,您将使用do {...} while(...); -请注意,您必须从伪代码中反转条件,因为它是while,而不是until

顺便说一句,当你自己抛出异常时,你可以跳过抛出,而不是让你的codelength保持一个你认为是无效的值,例如-1。无论如何,您都需要它,因为您必须捕获异常作为readNumber部件的一部分,该部件仍然在您的循环中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21205768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档