我的程序從用戶輸入,第一個數字,操作(+ - */^),然後是第二個數字。當我輸入5/0它說錯誤!不能被0除。這就是它應該做的。但是,當我鍵入5/5這是1我收到我的錯誤消息。嘗試並捕獲打印錯誤
do {
try {
if (opperation == "/" && num2 == 0);
throw new ArithmeticException();
} catch (ArithmeticException ae) {
System.out.println("ERROR !!! Cannot divide by 0");
}
System.out.println("Enter First Number");
num1 = scan.nextInt();
System.out.println("ENTER Opperation: ");
opperation = scan.next();
System.out.println("ENTER Second Number: ");
num2 = scan.nextInt();
} while (num2 == 0);
這個問題似乎是題外話,因爲它是又一個分號後if語句 –
此外,在什麼tbodt和我說上面,請參閱http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java –