简体   繁体   English

Java分数计算器-通过try / catch的DivideByZeroMethod

[英]Java Fraction Calculator - DivideByZeroMethod through a try/catch

I'm working on a program where the user inputs two different fractions and the computer then runs them through a Fraction method to test if they're equal or not. 我正在开发一个程序,用户输入两个不同的分数,然后计算机通过“分数”方法运行它们以测试它们是否相等。 An additional step in the assignment is to throw an IllegalArgumentException to identify if one of the denominators is 0 and in turn restart the program. 分配的另一步骤是引发IllegalArgumentException以确定分母之一是否为0,然后重新启动程序。 The Fraction and testFraction classes are as follows: 分数和testFraction类如下:

Fraction class: 分数等级:

class Fraction {

    private int numerator;
    private int denominator;

    Fraction() {
        numerator = 0;
        denominator = 1;

    }

    Fraction (int num, int den) {
        numerator = num;
        denominator = den;
        if (denominator == 0){
            throw new IllegalArgumentException (
            "Denominator cannot be zero");
        }
    }

    public void setNum(int num){
        numerator = num;
    }

    public void setDen(int den){
        denominator = den;

        }

    public int getNum(){
        return numerator;
    }

    public int getDen(){
        return denominator;
    }

    public String toString() {
        return numerator + "/" + denominator;
        }

    public boolean equals(Fraction other){
    if (numerator * other.getDen() == denominator * other.getNum()){
            return true;
         } 
        else {
            return false;
        }
    }
}

testFraction Class: testFraction类别:

import java.util.Scanner;

public class testFraction {

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in); 
    int a = 1;
    int b = 1;
    int c = 1;
    int d = 1;

    Fraction f1 = new Fraction(a, b);
    Fraction f2 = new Fraction(c, d);


    System.out.println("Enter the numerator of the first fraction: ");
    f1.setNum(keyboard.nextInt());

    System.out.println("Enter the denominator of the first fraction: ");
    f1.setDen(keyboard.nextInt());

    try{
        int fraction = a/b; 
    }
    catch (IllegalArgumentException e){
        System.out.println(e.getMessage);
        main(args);
    }

    System.out.println("\nEnter the numerator of the second fraction: ");
    f2.setNum(keyboard.nextInt());

    System.out.println("Enter the denominator of the second fraction: ");
    f2.setDen(keyboard.nextInt());

    System.out.println("\nFraction 1: " + f1);
    System.out.println("Fraction 2: " + f2);

    System.out.println("\nAre fractions equal? " + f1.equals(f2));

    System.out.println(("\nWould you like to run the counter with a different integer? \n(1 for YES - 0 for NO): "));
    int answer = keyboard.nextInt();

    if (answer == 1){
        System.out.println("\nLet's Rock 'n Roll! ");
        main(args);
    }

    else if (answer == 0){
        System.out.println("\nLater Dude! ");
        System.exit(0);
    }       
}
}

I feel like I'm incorrectly working in the try/catch. 我觉得我在尝试/捕获中工作不正确。 Thanks for your help. 谢谢你的帮助。

You're throwing an IllegalArgumentException when initializing your Fraction, yet you try to catch it when you do an actual division in testFraction. 初始化小数时,您将引发IllegalArgumentException,但是在testFraction中进行实际除法时,您尝试捕获它。 Your catch will never be reached, since the exception at initialization will stop the code from reaching the incorrect division. 永远不会到达您的陷阱,因为初始化时的异常将阻止代码到达错误的除法。 You should instead put your try/catch around the code creating Fractions. 相反,您应该将try / catch放在创建分数的代码周围。

Moreover, you initialize your Fractions with mock values : 1. This isn't really useful, and pose a problem : your constructor will never throw its IllegalArgumentException, since it is called with correct values. 此外,您可以使用模拟值初始化分数:1.这并不是真正有用,并且会带来一个问题:您的构造函数永远不会抛出其IllegalArgumentException,因为它使用正确的值进行了调用。 You should raise the IllegalArgumentException in your setDenominator instead, and call it from your constructor. 您应该改为在setDenominator中引发IllegalArgumentException,然后从构造函数中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM