java吧 关注:1,235,377贴子:12,705,161
  • 2回复贴,共1

java写计算器!求大神帮我改一个或提一个思路。

只看楼主收藏回复

=号输出结果的时候如果没有小数点以后的数就直接显示整数,楼下附目前的代码和运行问题。


IP属地:北京1楼2014-05-20 20:10回复
    if (e.getSource() == key.equal) {
    theSecondNum = Float.parseFloat(result.tf.getText());
    if (operation.equals("")) {
    result.tf.setText("0");
    }
    if (operation.equals("add"))
    result.tf.setText(String.valueOf(theFirstNum + theSecondNum));
    if (operation.equals("subtract"))
    result.tf.setText(String.valueOf(theFirstNum - theSecondNum));
    if (operation.equals("multiplay"))
    result.tf.setText(String.valueOf(theFirstNum * theSecondNum));
    if (operation.equals("divide")) {
    if (result.tf.getText().equals("0"))
    result.tf.setText("Error");
    else
    result.tf.setText(String
    .valueOf(theFirstNum / theSecondNum));
    }
    if (operation.equals("mod")) {
    if (result.tf.getText().equals("0"))
    result.tf.setText("Error");
    else
    result.tf.setText(String
    .valueOf(theFirstNum % theSecondNum));
    }
    isInit = true;
    theFirstNum = 0;
    theSecondNum = 0;
    operation = "";
    }


    IP属地:北京2楼2014-05-20 20:11
    回复



      IP属地:北京3楼2014-05-20 20:12
      回复