所以我在如何设置我正在写的程序上的try catch finally块时遇到了麻烦。我创建了三个名为MathOP,MathOP2和TestMathOP的类。现在我想添加try catch块来操作错误,以防用户输入字母作为输入并尝试将数字除以零。以下是我创建的课程。
MathOP.java
public class MathOP {
double MathAdd(double num1, double num2) {
return num1 + num2;
}
/**
* **********************
* This is the command for adding the first and second number.
*
* @param num1
* @param num2
* @return
*************************
*/
double MathSub(double num1, double num2) {
return num1 - num2;
}
}
MathOP2 .java
public class MathOP2 extends MathOP {
/**
*
* @param num1
* @param num2
* @return
*/
double MathMultiply(double num1, double num2) {
return num1 * num2;
}
/**
* **********************
* This is the command for multiplying the first and second number.
*
* @param num1
* @param num2
*************************
*/
double MathDivide(double num1, double num2) {
return num1 / num2;
}
}
TestMathOP .java
import java.util.Scanner;
class TestMathOP {
public static void main(String args[]) {
MathOP2 MathOP2Object = new MathOP2();
double firstNum, secondNum, sum;
char response;
char operator;
/**
* **********************
* Declare main class and set data types for double and char
*/
Scanner data = new Scanner(System.in);
do // do while loop
{
try {
/**
* **********************
* do while loop starts like this.
*/
System.out.print("Week #6 Assignment\n");
System.out.print("Test math operations for addition,"
+ "subtraction, multiplication and division\n");
System.out.print("Enter two numbers!\n");
System.out.print("Enter the First number >>\r\n");
firstNum = data.nextDouble();
/**
* **********************
* Here states that the user needs to enter two numbers. Asking
* the user to enter the first number to add
*/
System.out.print("\nEnter the Second number >>\r\n");
secondNum = data.nextDouble();
/**
* **********************
* Here the user needs to enter the second number.
*/
System.out.print("Enter operator >> +,-,* or /\r\n");
operator = data.next().charAt(0);
/**
* **********************
* Here the user has the choice which operation he/she wanter to
* use.
*/
switch (operator) {
case '+':
sum = MathOP2Object.MathAdd(firstNum, secondNum);
System.out.println("The answer is " + sum);
break;
/**
* **********************
* This is the command for adding the first and second
* number.
*/
case '-':
sum = MathOP2Object.MathSub(firstNum, secondNum);
System.out.println("The answer is " + sum);
break;
/**
* **********************
* This is the command for subtracting the first and second
* number.
*/
case '*':
sum = MathOP2Object.MathMultiply(firstNum, secondNum);
System.out.println("The answer is " + sum);
break;
/**
* **********************
* This is the command for multiplying the first and second
* number.
*/
case '/':
sum = MathOP2Object.MathDivide(firstNum, secondNum);
System.out.println("The answer is " + sum);
break;
/**
* **********************
* This is the command for dividing the first and second
* number.
*/
}
} catch (ArithmeticException e) {
System.out.println("You can't do that!" + e);
} catch (Exception e) {
System.out.println("You cannot enter letter(s)");
}
System.out.println("Do you want to exit (Y/N)?");
response = data.next().charAt(0);
/**
* **********************
* Asking the user to choose Y(yes) or N(no). Yes to exit and no to
* run the program again.
*/
if (response == 'N')// This is case sensitive!
{
System.out.print("\nRun the program again\r\n\n");
}
} while (response != 'Y'); // This is case sensitive!
System.out.print("\nThanks for using our system");
}
}
不知何故,程序没有按照我想要的方式运行。
提前非常感谢你!