2013-07-01 46 views
-1

我的計算器終止,只要除以0或使用非整數。我無法讓我的while循環工作。Java計算器,終止

任何人都可以幫忙,並解釋我如何解決這個問題?

編輯:

它仍然終止。

package b; 

import java.util.*; 


public class Calculator3{ 


    static boolean _state = true; 
    public static void main (String[] args){ 

     System.out.println("Usage: operand1 operator operand2"); 
     System.out.println("Operands are integers"); 
     System.out.println("Operators: + - * /"); 

     Scanner in = new Scanner(System.in); 

     do{ 


     int result = 0; 
     int operand1 = 0; 
     int operand2 = 0; 
     String operator = " "; 
     char op = ' '; 

     try{ 
     operand1 = in.nextInt(); 
     operator = in.next(); 
     op = operator.charAt(0); 
     operand2 = in.nextInt();} 

     catch (InputMismatchException e){ 
      System.out.println("One or both of the operands are non-integers. Please check your operands"); 
      break;} 





      try{ 
       switch (op){ 
        case '+': result = operand1 + operand2; 
        break; 
        case '-': result = operand1 - operand2; 
        break; 
        case '*': result = operand1 * operand2; 
        break; 
        case '/': result = operand1/operand2; 
        break; 
        default: System.out.println("Unknown Operator"); 
        break;} 

      } 

      catch(RuntimeException e){ 
       System.out.println("Operand2 cannot be 0"); 
       System.exit(0);} 


      finally{ 
       System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result);} 
     } 
      while (_state = true);}} 

回答

4
} while (_state = true); 

應該

} while (_state == true); 

或更好

} while (_state); 

你需要避免以下條件,以避免退出的形式循環早期:

  • 輸入值&運算符的順序不正確
  • 使用會導致ArithmeticException的表達式,例如3/0
+0

它仍然終止。 – user2540484

+3

@ user2540484 - 如果除以零,它總是會退出。你在catch塊中調用'System.exit()'。 – jahroy

+0

另外一種情況是,如果你沒有按照正確的順序輸入值 - 你將跳出循環(從異常塊) – Reimeus

1

如果您嘗試除以零,則會拋出異常。您的catch塊調用System.exit()

這就是爲什麼當您除以零時,您的代碼「終止」。

在將其用於算術之前,您應驗證您的輸入(確保它是實數)。

我會做這樣的事情:

do { 
    try { 
     char op = operator.charAt(0); 
     int operand1 = in.nextInt(); 
     int operand2 = in.nextInt(); 
     if (operand2 == 0 && op == '/') { 
      System.out.println("Cannot divide by zero"); 
      continue;  
     } 
     . 
     . 
     . 
     switch (op) { 
      . 
      . 
      . 
     } 
    } 
    catch (NumberFormatException nfe) { 
     System.out.println("Invalid input"); 
     continue; 
    } 
} while (_state); 

這只是許多可能的方法之一。

看看你的try/catch邏輯,並考慮拋出不同異常時應該發生什麼。

1

RuntimeException。 catch塊觸發,並執行System.exit(0)。想想你想如何處理錯誤。退出可能不是它。

0

試試這個

case '/': 
    if (operand2 == 0) { 
     System.out.println("Operand2 cannot be 0"); 
     continue; 
    } else { 
     result = operand1/operand2; 
     break; 
    } 
0

你的程序在2個條件退出。 首先是 catch (InputMismatchException e){ System.out.println("One or both of the operands are non-integers. Please check your operands"); break;}

如果你仍然想繼續在非整數的情況下,即雙,浮法刪除此休息等

二是

` catch(RuntimeException e){ 
      System.out.println("Operand2 cannot be 0"); 
      System.exit(0);}` 

System.exit(0)是導致此while循環中斷,如果要繼續,請刪除系統。出口(0);