【问题标题】:I keep getting Compile Errors on the Exceptions Handling Java code I am writing for class, can someone see what am I doing wrong? [closed]我在为班级编写的异常处理 Java 代码上不断收到编译错误,有人能看到我做错了什么吗? [关闭]
【发布时间】:2015-09-12 22:33:23
【问题描述】:

上周我有一个课堂作业,但我仍在试图弄清楚为什么我的代码无法无错误地编译。请帮助我理解我做错了什么。 (这里对编程很陌生)

本周的问题是:

首先,创建三个异常类,分别命名为 NumberHighException、NumberLowException 和 NumberNegativeException。 NumberHighException 和 NumberLowException 都应该直接从 Exception 类继承,但 NumberNegativeException 应该从 NumberLowException 继承。您可以使用此模块中定义的 BadDataException 类作为您的异常类的模型。

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

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

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

提示用户输入指定范围内的数字。 使用 Scanner 将用户输入读取为 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 添加到代码的最后一部分,它才开始出现错误。请帮忙。谢谢!

【问题讨论】:

  • 至少告诉我们您遇到的错误是什么,而不仅仅是“我遇到错误”。
  • 学会删除不良帖子。当每个人都对您投反对票时,这是有原因的。你甚至没有足够的回购来失去。
  • 请告诉您遇到的错误
  • 错误是:Program6.java:94: error: cannot find symbol Scanner input = new Scanner(System.in); ^ 符号:类扫描仪位置:类 Program5 Program6.java:94:错误:找不到符号扫描仪输入 = 新扫描仪(System.in); ^ 符号:类扫描仪位置:类 Program5 Program6.java:107:错误:找不到符号 catch(InputMismatchException e) ^ 符号:类 InputMismatchException 位置:类 Program5 3 个错误
  • @BeachLover8 你的 Java 知识、你的编程技能和你的学习状态与话题完全无关:How do I ask a good question? 简而言之:大幅减少文本。正确缩进你的代码。告诉确切的错误和它发生的行。尝试将您的代码最小化为可重现的示例。

标签: java exception exception-handling


【解决方案1】:

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

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);
    }
}

【讨论】:

  • 谢谢!!这确实有效。现在我只需要花点时间仔细看看我在做什么。
【解决方案2】:

你必须导入 Scanner 类:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-16
    • 2021-11-28
    • 2021-12-10
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    相关资源
    最近更新 更多