有没有方法来修复除以零的线程异常?

huangapple go评论96阅读模式
英文:

Is there a way to fix exception in thread for dividing zero?

问题

import java.util.Scanner;

public class MathTest 
{
    public static void main (String[] args)
    {
        Scanner input = new Scanner(System.in);
        
        System.out.print("Input integer value 1: ");
        int iNum1 = input.nextInt();
        System.out.print("Input integer value 2: ");
        int iNum2 = input.nextInt();
        System.out.print("Input double value 1: ");
        double dNum1 = input.nextDouble();
        System.out.print("Input double value 2: ");
        double dNum2 = input.nextDouble();
        
        int add = iNum1 + iNum2;
        int sub = iNum1 - iNum2;
        int multi = iNum1 * iNum2;
        int div = iNum1 / iNum2;
        int mod = iNum1 % iNum2;
        double add2 = dNum1 + dNum2;
        double sub2 = dNum1 - dNum2;
        double multi2 = dNum1 * dNum2;
        double div2 = dNum1 / dNum2;
        
        int add3 = iNum2 + iNum1;
        int sub3 = iNum2 - iNum1;
        int multi3 = iNum2 * iNum1;
        int div3 = iNum2 / iNum1;
        int mod3 = iNum2 % iNum1;
        double add4 = dNum2 + dNum1;
        double sub4 = dNum2 - dNum1;
        double multi4 = dNum2 * dNum1;
        double div4 = dNum2 / dNum1;
        
        if (iNum1 > iNum2 || dNum1 > dNum2)
        {
            System.out.println("\nInteger output:");
            System.out.printf("Adding " + iNum1 + " and " + iNum2 + " = " + add);
            System.out.printf("\nSubtracting " + iNum1 + " and "+ iNum2+ " = " + sub);
            System.out.printf("\nMultiplying " + iNum1 + " and "+ iNum2+ " = " + multi);
            System.out.printf("\nDividing " + iNum1 + " and " + iNum2 + " = " + div);
            System.out.printf("\nModding " + iNum1 + " and " + iNum2 + " = " + mod);
            
            System.out.println("\n\nDouble output:");
            System.out.printf("Adding " + dNum1 + " and " + dNum2 + " = %.2f%n", add2);
            System.out.printf("Subtracting " + dNum1 + " and " + dNum2 + " = %.2f%n", sub2);
            System.out.printf("Multiplying " + dNum1 + " and " + dNum2 + " = %.2f%n", multi2);
            System.out.printf("Dividing " + dNum1 + " and " +  dNum2 + " = %.2f%n", div2);
        }
        
        else if (iNum1 < iNum2 || dNum1 < dNum2)
        {
            System.out.println("\nInteger output:");
            System.out.printf("Adding " + iNum1 + " and " + iNum2 + " = " + add3);
            System.out.printf("\nSubtracting " + iNum1 + " and "+ iNum2+ " = " + sub3);
            System.out.printf("\nMultiplying " + iNum1 + " and "+ iNum2+ " = " + multi3);
            System.out.printf("\nDividing " + iNum1 + " and " + iNum2 + " = " + div3);
            System.out.printf("\nModding " + iNum1 + " and " + iNum2 + " = " + mod3);
            
            System.out.println("\n\nDouble output:");
            System.out.printf("Adding " + dNum1 + " and " + dNum2 + " = %.2f%n", add4);
            System.out.printf("Subtracting " + dNum1 + " and " + dNum2 + " = %.2f%n", sub4);
            System.out.printf("Multiplying " + dNum1 + " and " + dNum2 + " = %.2f%n", multi4);
            System.out.printf("Dividing " + dNum1 + " and " +  dNum2 + " = %.2f%n", div4);
        }
        
        if (iNum1 == iNum2 || dNum1 == dNum2)
        {
            System.out.println("\nInteger output:");
            System.out.printf("Adding " + iNum1 + " and " + iNum2 + " = " + add);
            System.out.printf("\nSubtracting " + iNum1 + " from itself " + " = " + sub);
            System.out.printf("\nSquaring " + iNum1 + " = " + multi);
            System.out.printf("\nDividing " + iNum1 + " and " + iNum2 + " = " + div);
            System.out.printf("\nModding " + iNum1 + " and " + iNum2 + " = " + mod);
            
            System.out.println("\n\nDouble output:");
            System.out.printf("Adding " + dNum1 + " and " + dNum2 + " = %.2f%n", add2);
            System.out.printf("Subtracting " + dNum1 + " from itself " + " = %.2f%n", sub2);
            System.out.printf("Squaring " + dNum1 + " = %.2f%n", multi2);
            System.out.printf("Dividing " + dNum1 + " and " +  dNum2 + " = %.2f%n", div2);
        }
        if (iNum2 == 0 || dNum2 == 0)
        {
            System.out.println("\nInteger output:");
            System.out.printf("Adding " + iNum1 + " and " + dNum2 + " = " + add);
            System.out.printf("\nSubtracting " + iNum1+ " and "+ dNum2 + " = " + sub);
            System.out.printf("\nMultiplying " + iNum1+ " and "+  dNum2 + " = " + multi);
            System.out.println("Error: You cannot divide and mod by zero!!!");
            
            System.out.println("\n\nDouble output:");
            System.out.printf("Adding " + dNum1 + " and " + dNum2 + " = " + add);
            System.out.printf("Subtracting " + dNum1 + " and " + dNum2 + " = " + sub);
            System.out.printf("Multiplying " + dNum1 + " and " + dNum2 + " = " + multi);
            System.out.println("Error: You cannot divide by zero!!!");
        }
        
    }
}
英文:

When I input 3,0,3.14,0, I get the error message. It seems like I'm diving by zero in lines 26,27, and 31. I know it is throwing the arithmetric exception. I need to use the if statements first to check for that case before I try to do any division that i'm doing after. So, the last if statement i have no idea what to do to fix it.

import java.util.Scanner;
public class MathTest 
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(&quot;Input integer value 1: &quot;);
int iNum1 = input.nextInt();
System.out.print(&quot;Input integer value 2: &quot;);
int iNum2 = input.nextInt();
System.out.print(&quot;Input double value 1: &quot;);
double dNum1 = input.nextDouble();
System.out.print(&quot;Input double value 2: &quot;);
double dNum2 = input.nextDouble();
int add = iNum1 + iNum2;
int sub = iNum1 - iNum2;
int multi = iNum1 * iNum2;
int div = iNum1 / iNum2;
int mod = iNum1 % iNum2;
double add2 = dNum1 + dNum2;
double sub2 = dNum1 - dNum2;
double multi2 = dNum1 * dNum2;
double div2 = dNum1 / dNum2;
int add3 = iNum2 + iNum1;
int sub3 = iNum2 - iNum1;
int multi3 = iNum2 * iNum1;
int div3 = iNum2 / iNum1;
int mod3 = iNum2 % iNum1;
double add4 = dNum2 + dNum1;
double sub4 = dNum2 - dNum1;
double multi4 = dNum2 * dNum1;
double div4 = dNum2 / dNum1;
if (iNum1 &gt; iNum2 || dNum1 &gt; dNum2)
{
System.out.println(&quot;\nInteger output:&quot;);
System.out.printf(&quot;Adding &quot; + iNum1 + &quot; and &quot; + iNum2 + &quot; = &quot; + add);
System.out.printf(&quot;\nSubtracting &quot; + iNum1+ &quot; and &quot;+ iNum2+ &quot; = &quot; + sub);
System.out.printf(&quot;\nMultiplying &quot; + iNum1+ &quot; and &quot;+ iNum2+ &quot; = &quot; + multi);
System.out.printf(&quot;\nDividing &quot; + iNum1 + &quot; and &quot; + iNum2 + &quot; = &quot; + div);
System.out.printf(&quot;\nModing &quot; + iNum1 + &quot; and &quot; + iNum2 + &quot; = &quot; + mod);
System.out.println(&quot;\n\nDouble output:&quot;);
System.out.printf(&quot;Adding &quot; + dNum1 + &quot; and &quot; + dNum2 + &quot; = %.2f%n&quot;, add2);
System.out.printf(&quot;Subtracting &quot; + dNum1 + &quot; and &quot; + dNum2 + &quot; = %.2f%n&quot;, sub2);
System.out.printf(&quot;Multiplying &quot; + dNum1 + &quot; and &quot; + dNum2 + &quot; = %.2f%n&quot;, multi2);
System.out.printf(&quot;Dividing &quot; + dNum1 + &quot; and &quot; +  dNum2 + &quot; = %.2f%n&quot;, div2);
}
else if (iNum1 &lt; iNum2 || dNum1 &lt; dNum2)
{
System.out.println(&quot;\nInteger output:&quot;);
System.out.printf(&quot;Adding &quot; + iNum1 + &quot; and &quot; + iNum2 + &quot; = &quot; + add3);
System.out.printf(&quot;\nSubtracting &quot; + iNum1 + &quot; and &quot;+ iNum2+ &quot; = &quot; + sub3);
System.out.printf(&quot;\nMultiplying &quot; + iNum1 + &quot; and &quot;+ iNum2+ &quot; = &quot; + multi3);
System.out.printf(&quot;\nDividing &quot; + iNum1 + &quot; and &quot; + iNum2 + &quot; = &quot; + div3);
System.out.printf(&quot;\nModing &quot; + iNum1 + &quot; and &quot; + iNum2 + &quot; = &quot; + mod3);
System.out.println(&quot;\n\nDouble output:&quot;);
System.out.printf(&quot;Adding &quot; + dNum1 + &quot; and &quot; + dNum2 + &quot; = %.2f%n&quot;, add4);
System.out.printf(&quot;Subtracting &quot; + dNum1 + &quot; and &quot; + dNum2 + &quot; = %.2f%n&quot;, sub4);
System.out.printf(&quot;Multiplying &quot; + dNum1 + &quot; and &quot; + dNum2 + &quot; = %.2f%n&quot;, multi4);
System.out.printf(&quot;Dividing &quot; + dNum1 + &quot; and &quot; +  dNum2 + &quot; = %.2f%n&quot;, div4);
}
if (iNum1 == iNum2 || dNum1 == dNum2)
{
System.out.println(&quot;\nInteger output:&quot;);
System.out.printf(&quot;Adding &quot; + iNum1 + &quot; and &quot; + iNum2 + &quot; = &quot; + add);
System.out.printf(&quot;\nSubtracting &quot; + iNum1 + &quot; from itself &quot;+ &quot; = &quot; + sub);
System.out.printf(&quot;\nSquaring &quot; + iNum1 + &quot; = &quot; + multi);
System.out.printf(&quot;\nDividing &quot; + iNum1 + &quot; and &quot; + iNum2 + &quot; = &quot; + div);
System.out.printf(&quot;\nModing &quot; + iNum1 + &quot; and &quot; + iNum2 + &quot; = &quot; + mod);
System.out.println(&quot;\n\nDouble output:&quot;);
System.out.printf(&quot;Adding &quot; + dNum1 + &quot; and &quot; + dNum2 + &quot; = %.2f%n&quot;, add2);
System.out.printf(&quot;Subtracting &quot; + dNum1 + &quot; from itself &quot; + &quot; = %.2f%n&quot;, sub2);
System.out.printf(&quot;Squaring &quot; + dNum1 + &quot; = %.2f%n&quot;, multi2);
System.out.printf(&quot;Dividing &quot; + dNum1 + &quot; and &quot; +  dNum2 + &quot; = %.2f%n&quot;, div2);
}
if (iNum2 == 0 || dNum2 == 0)
{
System.out.println(&quot;\nInteger output:&quot;);
System.out.printf(&quot;Adding &quot; + iNum1 + &quot; and &quot; + dNum2 + &quot; = &quot; + add);
System.out.printf(&quot;\nSubtracting &quot; + iNum1+ &quot; and &quot;+ dNum2 + &quot; = &quot; + sub);
System.out.printf(&quot;\nMultiplying &quot; + iNum1+ &quot; and &quot;+  dNum2 + &quot; = &quot; + multi);
System.out.println(&quot;Error: You cannot divide and mod by zero!!!&quot;);
System.out.println(&quot;\n\nDouble output:&quot;);
System.out.printf(&quot;Adding &quot; + dNum1 + &quot; and &quot; + dNum2 + &quot; = &quot; + add);
System.out.printf(&quot;Subtracting &quot; + dNum1 + &quot; and &quot; + dNum2 + &quot; = &quot; + sub);
System.out.printf(&quot;Multiplying &quot; + dNum1 + &quot; and &quot; + dNum2 + &quot; = &quot; + multi);
System.out.println(&quot;Error: You cannot divide by zero!!!&quot;);
}
}
}

答案1

得分: 1

当你进行除法运算时,你可以像这样做。它会静默失败。

double dNum2 = 10;
double dNum1 = 0;
double div4 = 0;
try {
    div4 = dNum2 / dNum1;
} catch (ArithmeticException ae) {
}

通常情况下,忽略捕获到的异常被认为是不良实践。但由于你在下面提到了这一点,所以应该是可以的。

英文:

You can do something like this when you do the division. It will silently fail.

double dNum2 = 10;
double dNum1 = 0;
double div4 = 0;
try {
div4 = dNum2 / dNum1;
} catch (ArithmeticException ae) {
}

Normally it is considered bad practice to ignore caught exceptions. But since you are warning about it below, it should be okay.

huangapple
  • 本文由 发表于 2020年9月8日 06:47:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63785101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定