2017-07-02 7 views
0

私はUniの学生であり、プログラミングの課題を持っています。私たちは、Javaを使用して電卓を作成しなければならず、ポイントの1つは、ユーザーがゼロで割ったときに「数字ではない」というメッセージを入力することです。通常は「Infinity」と表示されているので、そのメッセージを変更するのが目的ですが、これまで行っていなかった方法です。誰かがアイデアを持っている、または私がそれを書き直すのを助けることができるなら、ここでコメントしてください。ありがとうございました!Java Calculator ActionListener。ゼロで割ったときのメッセージ "Infinity"の変更方法は?

public void getResult() { 

double result = 0; 

temporary[1] = Double.parseDouble(display.getText()); 

String temp0 = Double.toString(temporary[0]); 

String temp1 = Double.toString(temporary[1]); 

    try { 
    if(function[2] == true) 
     result = temporary[0] * temporary[1]; 

    else if(function[3] == true) 
     result = temporary[0]/temporary[1]; 

    else if(function[0] == true) 
     result = temporary[0] + temporary [1]; 

    else if(function[1] == true) 
     result = temporary[0] - temporary[1];   
    display.setText(Double.toString(result)); 

    for(int i=0; i<4; i++) 
     function[i] = false; 
    } catch(NumberFormatException e) {} 
} 



public void actionPerformed(ActionEvent ae) { 
if(ae.getSource() == button[0]) 
    display.append("1"); 
if(ae.getSource() == button[1]) 
    display.append("2"); 
if(ae.getSource() == button[2]) 
    display.append("3"); 
if(ae.getSource() == button[3]) { 
    temporary[0] = Double.parseDouble(display.getText()); 
    function[0] = true; 
    display.setText(""); 
} 
if(ae.getSource() == button[4]) 
    display.append("4"); 
if(ae.getSource() == button[5]) 
    display.append("5"); 
if(ae.getSource() == button[6]) 
    display.append("6"); 
if(ae.getSource() == button[7]) { 
    temporary[0] = Double.parseDouble(display.getText()); 
    function[1] = true; 
    display.setText(""); 
} 
if(ae.getSource() == button[8]) 
    display.append("7"); 
if(ae.getSource() == button[9]) 
    display.append("8"); 
if(ae.getSource() == button[10]) 
    display.append("9"); 
if(ae.getSource() == button[11]) { 
    temporary[0] = Double.parseDouble(display.getText()); 
    function[2] = true; 
    display.setText(""); 
} 
if(ae.getSource() == button[12]) 
    display.append("0"); 
if(ae.getSource() == button[13]) 
    display.append("."); 
if(ae.getSource() == button[14]) 
    clear(); 
if(ae.getSource() == button[15]){ 
    temporary[0] = Double.parseDouble(display.getText()); 
    function[3] = true; 
    display.setText(""); 
} 
if(ae.getSource() == button[16]) 
    getResult(); 
} 

public static void main(String[] arguments) { 
    Calculator c = new Calculator(); 
} 
} 
+0

を(指揮==真) '(または' falseの場合は '絶対に使用しないでください'):' if(cond) '(あるいは' if(!cond) ')が簡単で、エラーが起こりにくい。 –

+0

このメソッドの結果を確認してください[Double#isInfinite()](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#isInfinite-double-) – matoni

+1

除算を行う前に 'temporary [1]'の値?あなたは明らかに 'if'と' else'に精通しています。 –

答えて

0

分母がゼロであることはご存知のように、無限大です。 Javaでは、プリミティブラッパークラスのためにゼロで割った場合、エラーはありません。例えば

public static void main(String[] args) { 
    Double a = 1.0; 
    Double b = 0.0; 

    Double c = a/b; 
    c.isInfinite(); 
    System.out.println(c.toString()); 
} 

プリント:

Infinity 

SO、ちょうどダブルに結果フォームの二重を変更すると:

public void getResult() { 
    Double result = 0; 
    temporary[1] = Double.parseDouble(display.getText()); 
    String temp0 = Double.toString(temporary[0]); 
    String temp1 = Double.toString(temporary[1]); 
    try { 
     if (function[2] == true) 
      result = temporary[0] * temporary[1]; 

     else if (function[3] == true) 
      result = temporary[0]/temporary[1]; 

     else if (function[0] == true) 
      result = temporary[0] + temporary[1]; 

     else if (function[1] == true) 
      result = temporary[0] - temporary[1]; 

     if(c.isInfinite() || c.isNaN()) { 
      display.setText("Not a number"); 
     } else { 
      display.setText(result.toString()); 
     } 
     for (int i = 0; i < 4; i++) 
      function[i] = false; 
    } catch (NumberFormatException e) { 
     display.setText(e.getMessage()); 
    } 
} 
+2

_In Javaでは、ゼロで除算するとエラーはありません。真ではありません。 'int i = 3/0'を実行して、あなたが得るものを見てください。 –

+0

ありがとう@مصطفی、あなたの例は私の問題を解決! –

関連する問題