Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 大学作业的异常处理_Java - Fatal编程技术网

Java 大学作业的异常处理

Java 大学作业的异常处理,java,Java,我现在正在做一项关于例外情况的大学作业。我写了一些代码,但它不起作用,只是想知道是否有人能发现我哪里出了问题?任何帮助都将不胜感激 我专门编写了一个类来处理InputMismatchException,但是,当我在数组中输入double而不是整数时,程序仍然崩溃,而不是像我希望的那样处理异常 谢谢 public class Lab8 { public static void main(String[] args) { ArrayList<Integer> a

我现在正在做一项关于例外情况的大学作业。我写了一些代码,但它不起作用,只是想知道是否有人能发现我哪里出了问题?任何帮助都将不胜感激

我专门编写了一个类来处理InputMismatchException,但是,当我在数组中输入double而不是整数时,程序仍然崩溃,而不是像我希望的那样处理异常

谢谢

public class Lab8 {
    public static void main(String[] args) {
        ArrayList<Integer> a = new ArrayList<Integer>();
        Scanner s = new Scanner(System.in);
        boolean x = true;
        while (x == true) {
            try {
                System.out.println("Enter the next integer: ");
                /*Autoboxing takes place here. The primitive type "int" taken in from the
                user is converted to an Integer object.*/
                Integer i = s.nextInt();
                a.add(i);
            } catch (InputMismatchException e) {
                System.out.println("You must enter an integer.");
            }

            System.out.println("Would you like to enter another integer? (Y/N): ");
            char y_n = s.next().charAt(0);
            if (y_n == 'Y') {
                x = true;
            } else {
                x = false;
            }
        }
        for (int i = 0; i < a.size(); i++) {
            System.out.println(a.get(i));
        }

    }
}
公共类Lab8{
公共静态void main(字符串[]args){
ArrayList a=新的ArrayList();
扫描仪s=新的扫描仪(System.in);
布尔x=真;
while(x==true){
试一试{
System.out.println(“输入下一个整数:”);
/*自动装箱发生在这里。原语类型“int”取自
用户已转换为整数对象*/
整数i=s.nextInt();
a、 加(i);
}捕获(输入不匹配异常e){
System.out.println(“您必须输入一个整数。”);
}
System.out.println(“是否要输入另一个整数?(Y/N):”;
char y_n=s.next().charAt(0);
如果(y_n=='y'){
x=真;
}否则{
x=假;
}
}
对于(int i=0;i
只需添加

s = new Scanner(System.in);
像这样

System.out.println("Would you like to enter another integer? (Y/N): ");
s = new Scanner(System.in);
char y_n = s.next().charAt(0);

因为扫描仪无法从输入中提取int,所以它仍然卡在那里。在寻找新内容之前,您需要清除输入;或者
next.charAt(0)
将是双精度的第一位(在您的示例中)。在catch子句中,添加

s.next();

听起来好像应用程序引发了另一个您没有捕获的异常,您可以始终使用exception而不是InputMisMatchException来捕获所有异常。当您在调试器中单步执行此操作时,抛出的异常是什么,抛出的异常是哪一行?