简体   繁体   English

尝试使用Java循环捕获

[英]Try Catch With Loop Java

I'm new Java student. 我是新来的Java学生。 Maybe I don't understand how try/catch really works. 也许我不明白try / catch的工作原理。 I'm making a Java class game, the hangman. 我正在制作Java类游戏,子手。 And I'm making a method that returns the number of players. 我正在制定一种返回玩家人数的方法。 I have another code that works perfectly. 我有另一个完美的代码。 Just if I introduce letters and no Int, it crash. 只是如果我引入字母而没有Int,它就会崩溃。 I tried to make this one. 我试图做到这一点。 I hope you understand me. 我希望你能理解我。

public static int setNumJugadores() {
    Scanner sc = new Scanner(System.in);
    int numJugador=0;
    System.out.println("Introduzca el número de jugadores a jugar: ");
    // VARIABLES PARA EL TRY CATCH


    boolean bError=false;
    boolean mayorQueCero=false;
    do {
        try{
            numJugador = sc.nextInt();
        }
        catch (Exception e){
            bError=true;
            System.out.println("Error, introduzca un numero entero.");
        }

        if (numJugador < 1) {
            System.out.println("ERROR, introduzca un valor valido mayor de 0");
        }
        else{
            mayorQueCero=true;
        }
    } while ((!mayorQueCero)||(!bError));

    return numJugador;
}       

The problem is, if someone enters letters, the exception will in fact be thrown, and you will catch it. 问题是,如果有人输入字母,则实际上会引发异常,而您会抓住它。 However those characters are not removed from the input. 但是,这些字符不会从输入中删除。 So the next time you try to read the input, you will still have characters, and the exception will be thrown again. 因此,下次尝试读取输入时,您仍然会遇到字符,并且异常将再次引发。

Add a sc.nextLine(); 添加一个sc.nextLine(); in the catch block to eat up the bad line of data. 在catch块中吃掉坏数据行。

The problem here is 这里的问题是

nextInt() : Scans the next token of the input as an int nextInt():将输入的下一个标记扫描为int

When you enter character it throws exception and goes to catch block and the program crashes. 当您输入字符时,它将引发异常并进入catch块,并且程序崩溃。

Use the following code 使用以下代码

public static int setNumJugadores() {
    Scanner sc = new Scanner(System.in);
    String numJugador=null;
    System.out.println("Introduzca el número de jugadores a jugar: ");
    // VARIABLES PARA EL TRY CATCH


    boolean bError=false;
    boolean mayorQueCero=false;
    do {
        try{
            numJugador = sc.nextLine();
        }
        catch (Exception e){
            bError=true;
            numJugador = null;
            System.out.println("Error, introduzca un numero entero.");
        }

        if (numJugador == null) {
            System.out.println("ERROR, introduzca un valor valido mayor de 0");
        }
        else{
            mayorQueCero=true;
        }
    } while ((!mayorQueCero)||(!bError));

    return numJugador;
}      

nextLine() : Advances this scanner past the current line and returns the input that was skipped. nextLine():使此扫描程序前进到当前行之外,并返回被跳过的输入。

See the Scanner Java Doc for details. 有关详细信息,请参见Scanner Java Doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM