我在为 class 编写的异常处理 Java 代码中不断收到编译错误,有人能看出我做错了什么吗?

I keep getting Compile Errors on the Exceptions Handling Java code I am writing for class, can someone see what am I doing wrong?

上周我有一个 class 的作业,但我仍在努力弄清楚为什么我的代码无法正确编译。请帮助我了解我做错了什么。 (在这里编程很新)

本周的问题是:

首先,创建三个异常 classes,分别命名为 NumberHighException、NumberLowException 和 NumberNegativeException。 NumberHighException 和 NumberLowException 都应该直接从 Exception class 子classed,但是 NumberNegativeException 应该从 NumberLowException 子classed。您可以使用在此模块中定义的 BadDataException class 作为异常 classes.

的模型

接下来创建一个名为 Verify 的 class,它将用于验证数字是否在指定范围内。它应该有一个具有两个 int 参数的构造函数。第一个参数为范围内的最小数,第二个参数为范围内的最大数

除了构造函数之外,Verify class 应该有一个名为 validate 的方法。验证方法应该有一个数据类型为 int 的参数。该参数包含正在验证的数字。如果参数的值小于零,该方法应该抛出 NumberNegativeException。如果该值小于范围的最小值,则应抛出 NumberLowException。如果该值大于范围的最大值,则应抛出 NumberHighException。如果该值在指定范围内,则不应抛出异常。

创建所有这些 classes 后,创建名为 Program5 的驱动程序 class。驱动程序 class 应该实例化一个范围为 10 到 100 的验证对象。然后它应该执行以下操作:

提示用户输入指定范围内的数字。 使用扫描仪将用户输入读取为 int。您可以确保输入了一个 int,因为如果输入任何非数字,nextInt 方法将抛出 InputMismatchException。 调用 validate 方法来验证数字是否在范围内。 如果值不在范围内,则打印相应的错误消息,如果值在范围内,则打印该值。

我能够创建的代码如下所示:

class NumberHighException extends Exception {
    public NumberHighException() {
    }

    public NumberHighException(String str) {
        super(str);
    }

    public String toString() {
        return "NumberHighException";
    }
}

class NumberLowException extends Exception {
    public NumberLowException() {
    }

    public NumberLowException(String str) {
        super(str);
    }

    public String toString() {
        return "NumberLowException";
    }
}

// negative number is a type of low number
class NumberNegativeException extends NumberLowException {

    public NumberNegativeException() {
    }

    public NumberNegativeException(String str) {
        super(str);
    }

    public String toString() {
        return "NumberNegativeException";
    }
}

class Verify {
    // lowest number in range
    private int minimum;
    // highest number in range
    private int maximum;

    // constructor sets minimum and maximum values in range
    public Verify(int minimum, int maximum) {
        this.minimum = minimum;
        this.maximum = maximum;
    }

    // validate that number is within range
    public void validate(int number) throws NumberNegativeException, NumberLowException, NumberHighException

    {
        if (number < 0)
            throw new NumberNegativeException("number < 0");
        else if (number < minimum)
            throw new NumberLowException("number < 10");
        else if (number > maximum)
            throw new NumberHighException("number > 100");
    }
}

class Program5 {
    public static void main(String[] args) {
        int number = 0;
        int returnCode = 0;
        int minimum = 10;
        int maximum = 100;

        // create object to verify number is within range 10 to 100
        Verify ok = new Verify(minimum, maximum);

        // create Scanner object to read keyboard
        Scanner input = new Scanner(System.in);

        // prompt for input
        System.out.print("Enter number between " + minimum + " and " + maximum + ": ");

        try {
            // read int from keyboard
            // throws exception if non digits are entered
            number = input.nextInt();

        } catch (InputMismatchException e) {
            System.err.println("You entered a non digit");
            System.exit(1);
        }

        // validate that entered number is within specified range
        try {
            ok.validate(number);
        } catch (NumberHighException e) {
            System.out.println("NumberHighException: " + e.getMessage());
            returnCode = 2;
        }

        // because NumberNegativeException is subclass of NumberLowException
        // it must be caught before NumberLowException or compile error results
        catch (NumberNegativeException e) {
            System.out.println("NumberNegativeException: " + e.getMessage());
            returnCode = 3;
        }

        catch (NumberLowException e) {
            System.out.println("NumberLowException: " + e.getMessage());
            returnCode = 4;
        }

        finally {
            // true is number entered was within range
            if (returnCode == 0)
                System.out.println(number + " is valid number");
        }
        System.exit(returnCode);
    }
}

我一步一步检查了每个部分,直到最后一部分我将Program5添加到代码中时才开始出错。请帮忙。谢谢!

看看这段代码...也许它可以帮助你...

import java.util.InputMismatchException; //added the import
import java.util.Scanner; // added the import

class NumberHighException extends Exception {
    public NumberHighException() {}
    public NumberHighException(String str) {
        super(str);
    }
    public String toString() {
        return "NumberHighException";
    }
}

class NumberLowException extends Exception {
    public NumberLowException() {}
    public NumberLowException(String str) {
        super(str);
    }
    public String toString() {
        return "NumberLowException";
    }
}

// negative number is a type of low number 
class NumberNegativeException extends NumberLowException {

    public NumberNegativeException() {}
    public NumberNegativeException(String str) {
        super(str);
    }
    public String toString() {
        return "NumberNegativeException";
    }
}

class Verify {
    // lowest number in range    
    private int minimum;
    // highest number in range    
    private int maximum;
    // constructor sets minimum and maximum values in range    
    public Verify(int minimum, int maximum) {
        this.minimum = minimum;
        this.maximum = maximum;
    }
    // validate that number is within range    
    public void validate(int number)
    throws NumberNegativeException, NumberLowException, NumberHighException

    {
        if (number < 0) throw new NumberNegativeException("number < 0");
        else if (number < minimum) throw new NumberLowException("number < 10");
        else if (number > maximum) throw new NumberHighException("number > 100");
    }
}

public class Program5 // added the public keyword
{
    public static void main(String[] args) {
        int number = 0;
        int returnCode = 0;
        int minimum = 10;
        int maximum = 100;

        // create object to verify number is within range 10 to 100       
        Verify ok = new Verify(minimum, maximum);

        // create Scanner object to read keyboard       
        Scanner input = new Scanner(System. in );
        // prompt for input       
        System.out.print("Enter number between " + minimum +
            " and " + maximum + ": ");

        try {
            // read int from keyboard          
            // throws exception if non digits are entered          
            number = input.nextInt();

        } catch (InputMismatchException e) {
            System.err.println("You entered a non digit");
            System.exit(1);
        }

        // validate that entered number is within specified range              
        try {
            ok.validate(number);
        } catch (NumberHighException e) {
            System.out.println("NumberHighException: " + e.getMessage());
            returnCode = 2;
        }

        // because NumberNegativeException is subclass of NumberLowException       
        // it must be caught before NumberLowException or compile error results       
        catch (NumberNegativeException e) {
            System.out.println("NumberNegativeException: " + e.getMessage());
            returnCode = 3;
        } catch (NumberLowException e) {
            System.out.println("NumberLowException: " + e.getMessage());
            returnCode = 4;
        } finally {
            // true is number entered was within range          
            if (returnCode == 0) System.out.println(number + " is valid number");
        }
        System.exit(returnCode);
    }
}

您必须导入 Scanner class:

import java.util.Scanner;
class Program5  
{    
public static void main(String [] args)