java - 计算器不能有小数

标签 java decimal calculator

我的代码现在适用于求和、减法、乘法、除法。 但是,当我输入 10.0 和 2.0 或 5.4 和 2 时,它不显示小数。 应该怎么改才可以用十进制呢?

这是我现在的代码。

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class Calculator extends JFrame implements ActionListener {

    // instance variables
    JLabel num1Label = null;
    JTextField num1Text = null;
    JLabel num2Label = null;
    JTextField num2Text = null;
    JButton sumButton = null;
    JButton multipleButton=null;
    JButton divideButton=null;
    JButton subtractButton = null;
    JLabel ansLabel = null;

    // constructor
    public Calculator() {
        setLayout(new GridBagLayout());    // set layout (how elements are arranged in window)
        // ****  label:  num 1
        GridBagConstraints layoutCon = new GridBagConstraints();
        layoutCon.gridx = 0;
        layoutCon.gridy = 0;
        layoutCon.insets = new Insets(10,10,10,10);

        num1Label = new JLabel("Num 1:");
        add(num1Label,layoutCon);   // added label to JFrame


        // ****  text field:  num 1
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 1;
        layoutCon.gridy = 0;
        layoutCon.insets = new Insets(10,10,10,10);

        num1Text = new JTextField(20);
        add(num1Text, layoutCon);

        // ****  label:  num 2
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 0;
        layoutCon.gridy = 1;
        layoutCon.insets = new Insets(10,10,10,10);

        num2Label = new JLabel("Num 2:");
        add(num2Label,layoutCon);   // added label to JFrame


        // ****  text field: num 2
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 1;
        layoutCon.gridy = 1;
        layoutCon.insets = new Insets(10,10,10,10);

        num2Text = new JTextField(20);
        add(num2Text, layoutCon);




        // ****  Sum Button
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 0;
        layoutCon.gridy = 2;
        layoutCon.insets = new Insets(10,10,10,10);

        sumButton = new JButton("Sum");
        add(sumButton, layoutCon);
        sumButton.addActionListener(this);  // register sumButton with the ActionListener in Calculator

        // Multiple Button
        layoutCon= new GridBagConstraints();
        layoutCon.gridx=0;
        layoutCon.gridy=4;
        layoutCon.insets=new Insets(10,10,10,10);

        multipleButton= new JButton("Multiple");
        add(multipleButton, layoutCon);
        multipleButton.addActionListener(this);

        // Divide Button
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 0;
        layoutCon.gridy = 5;
        layoutCon.insets = new Insets(10,10,10,10);

        divideButton = new JButton("Divide");
        add(divideButton, layoutCon);
        divideButton.addActionListener(this);

        // ****  label:  answer
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 1;
        layoutCon.gridy = 2;
        layoutCon.insets = new Insets(10,10,10,10);

        ansLabel = new JLabel("Answer");
        add(ansLabel,layoutCon);   // added label to JFrame  





        // ****  Subtract Button
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 0;
        layoutCon.gridy = 3;
        layoutCon.insets = new Insets(10,10,10,10);

        subtractButton = new JButton("Subtract");
        add(subtractButton, layoutCon);
        subtractButton.addActionListener(this);  // register subtractButton with the ActionListener in Calculator

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Sum")) {
            ansLabel.setText("" + (Integer.parseInt(num1Text.getText()) + Integer.parseInt(num2Text.getText())));
        }
        else if (e.getActionCommand().equals("Subtract")) {
            ansLabel.setText("" + (Integer.parseInt(num1Text.getText()) - Integer.parseInt(num2Text.getText())));
        }
        else if (e.getActionCommand().equals("Multiple")){
            ansLabel.setText(""+(double)(Integer.parseInt(num1Text.getText())*Integer.parseInt(num2Text.getText())));
        }
        else if (e.getActionCommand().equals("Divide")){
            ansLabel.setText(""+(Integer.parseInt(num1Text.getText())/Integer.parseInt(num2Text.getText())));

        }
        }

    public static void main(String[] args) {
        Calculator calc = new Calculator();

        calc.pack();    // resizes window (JFrame) so you can see the elements in it
        calc.setVisible(true);   // make window visible
        calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit program when close window
    }

}

最佳答案

actionPerformed 将其输入解析为 int 并对其进行操作。如果您想使用 float ,请尝试使用double

关于java - 计算器不能有小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33192692/

相关文章:

java - 为什么 cflock 有时会导致 'timeout value is negative' 错误?

java - 将 Wildfly 14 双向 SSL 限制为特定客户端

java - 如何用 Java 创建运费计算器

C 语言计算器 - 限制输入值

javascript - 当所有变量和输入都是数字时,为什么我得到 NaN?

java - 命令是否从 shell 脚本中的当前目录运行?

java - 如何让 IntelliJ 识别多个图 block views.xml?

javascript - 你能将一个变量除/乘另一个数字四舍五入到最接近的百分之几吗?

数字系统代码中的Java数字格式异常错误

c# - 将(长尾数)和(sbyte 指数)转换为十进制