英文:
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("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("\nModing " + 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("\nModing " + 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("\nModing " + 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!!!");
}
}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论