首页 > 解决方案 > 最近捕获的异常下的所有 Numberformat 异常都将自行执行

问题描述

我的程序获取两个学生的姓名和三个科目的分数并计算学生的平均值。在中间,如果标记不是整数和其他两个用户定义的异常,我必须捕获 numberFormatexception。每当捕获到 numberFormatexception 时,下面的其他 numberFormatecxceptions 就会自行捕获。

import java.util.*;

class ude1 extends Exception
{
    public ude1()
    {
        System.out.println("User defind exception 1 thrown");
    }
    public String toString()
    {
        return "NegativeValueException";
    }
}
class ude2 extends Exception
{
    public ude2()
    {
        System.out.println("User defind exception 2 thrown");
    }
    public String toString()
    {
        return "ValueOutofBouundException";
    }
}
class excep6
{
    /* String stud_name;
    int mark1,mark2,mark3;
    excep6(String name,int a,int b,int c)
    {
        stud_name=name;
        mark1=a;
        mark2=b;
        mark3=c;
    }
    public void calculator()
    {
      float avg=0;
      avg=(mark1+mark2+mark3)/3;
      System.out.println("The average of "+stud_name+" is "+avg);

    } */
    public static void main(String []args)
    {
        Scanner in = new Scanner(System.in);
        int a=0,b=0,c=0,l=2;
        String std="";

        while(l>0)
        {
    try{
        System.out.println("enter student name");
        std=in.next();
        System.out.println("enter mark1");

        if(in.hasNextInt())
            a=in.nextInt();
        else
            throw new NumberFormatException();
        if(a<0)
        {
            throw new ude1();
        }
        if(a>100)
        {
            throw new ude2();
        }
    }
    catch(ude1 u1)
        {
            System.out.println(u1.toString());a=0;
        }
    catch(ude2 u2)
        {
            System.out.println(u2.toString());a=0;
        }
    catch(NumberFormatException e)
        {
            System.out.println("NumberFormat Exception");a=0;
        }
        System.out.println("enter mark2");
    try{
        if(in.hasNextInt())
            b=in.nextInt();
        else
            throw new NumberFormatException();
        if(b<0)
        {
            throw new ude1();
        }
        if(b>100)
        {
            throw new ude2();
        }
    }
    catch(ude1 u1)
        {
            System.out.println(u1.toString());b=0;
        }
    catch(ude2 u2)
        {
            System.out.println(u2.toString());b=0;
        }
    catch(NumberFormatException e)
        {
            System.out.println("NumberFormatException Exception");b=0;
        }
        System.out.println("enter mark3");
    try{
        if(in.hasNextInt())
            c=in.nextInt();
        else
            throw new NumberFormatException();
        if(c<0)
        {
            throw new ude1();
        }
        if(c>100)
        {
            throw new ude2();
        }
        }
    catch(ude1 u1)
        {
            System.out.println(u1.toString());c=0;
        }
    catch(ude2 u2)
        {
            System.out.println(u2.toString());c=0;
        }
    catch(NumberFormatException e)
        {
            System.out.println("NumberFormatException Exception");c=0;
        }
        System.out.println("The average of student "+std+" is "+(a+b+c)/3);
        l--;
        }
    }
}

我预计

enter the name 
sat
enter mark1
i
NumberFormat exception
enter mark2
34
enter mark3
56
the avg is 30

而不是

enter the name 
sat
enter mark1
i
NumberFormat exception
enter mark2
NumberFormat exception
enter mark3
NumberFormat exception
enter the name
_

标签: javaexception

解决方案


如果你调用hasNextInt()它返回false,然后再次调用它,它保证false再次返回。

hasNextInt()不会跳过不是整数的标记。它留在扫描仪中,以便您可以尝试将其作为其他内容读取。

在大多数情况下,从意外输入中恢复的正确方法(例如,当预期为整数时为非整数)是调用nextLine()并丢弃结果。一个nextLine()调用将消耗所有字符,包括下一个行尾。


您还应该注意以下评论:

  • 遵循类名中的样式约定,以及
  • 不当使用自定义异常。

推荐阅读