我如何附加两个文件,它说要连接它们但它不能正常工作,而且我一直在收到错误

问题描述 投票:1回答:2

我试图呼吁DevideByZeroo,但它一直说不能找到符号。

我已经尝试过将它改成超级,我一直在变红。

package CatchBlock;
import java.util.Scanner;
import java.util.InputMismatchException;
/**
 *
 * @author Moe
 */
public class catchblock {
     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // TODO code application logic here
        boolean invalid = true;
        Scanner Pad = new Scanner(System.in);
        int i1 = 0, i2 = 0;
        double r = 0;
        while(invalid)
        {
            try{
                System.out.print("Enter first Integer: ");
                i1 = Pad.nextInt();
                System.out.print("Enter second integer: ");
                i2 = Pad.nextInt();
                if(i2 == 0)
                    DevideByZeroo("Cannot divide by Zero");
                r = (double) i1/i2;
                invalid = false;
            }
            catch(InputMismatchException e) {
                Pad.nextLine();
                System.out.println("Please enter a number.");
            }
            catch(DevideByZeroo e)
            {
                Pad.nextLine();
                System.out.println(e.GetMessage());
            }
        }
        System.out.println(i1 + " divided by " + i2 + " is " + r + ".");
    }

}

package CatchBlock;

/**
 *
 * @author Moe
 */
public class Divide extends catchblock {

    public String DevideByZeroo() {
        return  "Error: Dividing by Zero is impossible";
    }

    public String DevideByZeroo(String message){
        return message;
    }
}

它应该要求一些整数,并确保我们不是除以零。现在我遇到了能够在另一个文件中看到该类的问题。

java
2个回答
0
投票

由于您要进行自定义异常,因此您需要扩展Exception类。您还应该在自定义异常类的名称末尾添加“Exception”一词。所以你的DevideByZeroo类的代码应该是这样的: -

package CatchBlock;
public class DevideByZerooException extends Exception {

    DevideByZerooException() {
        super("Error: Dividing by Zero is impossible");
    }

    DevideByZerooException(String message){
        super(message);
    }
}

因此,catchblock类的代码也会像这样改变: -

package CatchBlock;
import java.util.Scanner;
import java.util.InputMismatchException;
/**
 *
 * @author Moe
 */
public class catchblock {
     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // TODO code application logic here
        boolean invalid = true;
        Scanner Pad = new Scanner(System.in);
        int i1 = 0, i2 = 0;
        double r = 0;
        while(invalid)
        {
            try{
                System.out.print("Enter first Integer: ");
                i1 = Pad.nextInt();
                System.out.print("Enter second integer: ");
                i2 = Pad.nextInt();
                if(i2 == 0)
                    throw new DevideByZerooException("Cannot divide by Zero");
                r = (double) i1/i2;
                invalid = false;
            }
            catch(InputMismatchException e) {
                Pad.nextLine();
                System.out.println("Please enter a number.");
            }
            catch(DevideByZerooException e)
            {
                Pad.nextLine();
                System.out.println(e.getMessage());
            }
        }
        System.out.println(i1 + " divided by " + i2 + " is " + r + ".");
    }
}

作为良好实践的一部分,您应该将包名称保持为小写并且使用大写字母和类来启动类名称应该优选为名词。


0
投票

您正在尝试使用不存在的异常类的名称。

  • 拼写错了。 “除以零”不是“除以零”
  • 任何标准例外都将以ExceptionError结束。
  • 实际上,除以零时抛出的标准异常是ArithmeticException;看到javadoc

如果DevideByZeroo应该是一个自定义的例外:

  • 这不是你抛出和异常的方式: DevideByZeroo("Cannot divide by Zero"); 它应该是: throw new DevideByZeroo("Cannot divide by Zero");
  • 你需要声明DevideByZeroo类。如果你在与主类相同的包中声明它,那么你将不需要import它。 (你没有“附加”类!)

当现有标准类已存在时,通常声明自定义类通常是一个坏主意。但是,如果您确实定义了自定义类,则应遵循Java约定:

  • 例外名称以Exception结尾。
  • 拼写应该使用您正在使用的语言。

因为在类和方法名称中看到错误的拼写真的很棒。这就像一个小孩在黑板上刮指甲!如果您的拼写不好,请使用在线词典。


我注意到这个:

public class Divide extends catchblock {
   ...
}

这不是声明异常类。


你在这里犯了很多错误,我认为你能做的最好的事情是阅读关于异常的整个Oracle Java教程课程:

这解释了如何声明自定义异常类。阅读!

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.