Chaibi JAVA"AWT-EventQueue-0"java.lang.NumberFormatException:多个点

Cha*_*laa 1 java swing

我是第一次练习JAVA的学生.我们被要求创建一个简单的计算器.我做了一切没有任何错误.我的唯一问题是,当我以1.66 + 55为例,甚至5 + 6时,它会显示标题中的错误.这是同志

import java.awt.BorderLayout;
Run Code Online (Sandbox Code Playgroud)

公共类Frame2扩展JFrame {

private JPanel contentPane;
private JTextField tot;
public String operateur = ""; 
public String s;
public String Value = "";
public String Value2 = "";
public Double Total = 0.0;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Frame2 frame = new Frame2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Frame2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 267, 360);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    tot = new JTextField();
    tot.setFont(new Font("Tahoma", Font.PLAIN, 18));
    tot.setEditable(false);
    tot.setBounds(10, 11, 231, 45);
    contentPane.add(tot);
    tot.setColumns(10);

    JButton t7 = new JButton("7");
    t7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 7;
            } else {
            Value2 = Value2.trim() + 7;
            }
            s = tot.getText();
            tot.setText(s+"7");
        }
    });
    t7.setBounds(10, 67, 49, 41);
    contentPane.add(t7);

    JButton t8 = new JButton("8");
    t8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if (operateur == "") { 
            Value = Value.trim() + 8;
            } else {
            Value2 = Value2.trim() + 8;
            }
            s = tot.getText();
            tot.setText(s+"8");


        }
    });
    t8.setBounds(69, 67, 50, 41);
    contentPane.add(t8);

    JButton t9 = new JButton("9");
    t9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 9;
            } else {
            Value2 = Value2.trim() + 9;
            }
            s = tot.getText();
            tot.setText(s+"9");
        }
    });
    t9.setBounds(129, 67, 50, 41);
    contentPane.add(t9);

    JButton t4 = new JButton("4");
    t4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 4;
            } else {
            Value2 = Value2.trim() + 4;
            }
            s = tot.getText();
            tot.setText(s+"4");
        }
    });
    t4.setBounds(10, 119, 50, 41);
    contentPane.add(t4);

    JButton t5 = new JButton("5");
    t5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 5;
            } else {
            Value2 = Value2.trim() + 5;
            }
            s = tot.getText();
            tot.setText(s+"5");
        }
    });
    t5.setBounds(69, 119, 50, 41);
    contentPane.add(t5);

    JButton t6 = new JButton("6");
    t6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 6.0;
            } else {
            Value2 = Value2.trim() + 6.0;
            }
            s = tot.getText();
            tot.setText(s+"6");
        }
    });
    t6.setBounds(129, 119, 50, 41);
    contentPane.add(t6);

    JButton t3 = new JButton("3");
    t3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 3.0;
            } else {
            Value2 = Value2.trim() + 3.0;
            }
            s = tot.getText();
            tot.setText(s+"3");
        }
    });
    t3.setBounds(10, 173, 49, 39);
    contentPane.add(t3);

    JButton t2 = new JButton("2");
    t2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 2;
            } else {
            Value2 = Value2.trim() + 2;
            }
            s = tot.getText();
            tot.setText(s+"2");
        }
    });
    t2.setBounds(69, 171, 50, 41);
    contentPane.add(t2);

    JButton t1 = new JButton("1");

    t1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value + 1;
            } else if (operateur != null) {
            Value2 = Value2.trim() + 1;
            }
            s = tot.getText();
            tot.setText(s+"1");


        }
    });
    t1.setBounds(129, 171, 49, 41);
    contentPane.add(t1);

    JButton t0 = new JButton("0");
    t0.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 0;
            } else {
            Value2 = Value2.trim() + 0;
            }
            s = tot.getText();
            tot.setText(s+"0");
        }
    });
    t0.setBounds(129, 221, 50, 41);
    contentPane.add(t0);

    JButton virg = new JButton(",");
    virg.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
                Value = Value.trim() + ".";
                } else {
                Value2 = Value2.trim() + ".";
                }
                s = tot.getText();
                tot.setText(s+".");

        }
    });
    virg.setBounds(69, 223, 50, 39);
    contentPane.add(virg);

    JButton btnC = new JButton("C");
    btnC.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tot.setText("");
            operateur = "";
            Value = "";
            Value2 = "";
            Total = 0.0;

        }
    });
    btnC.setForeground(Color.RED);
    btnC.setBounds(10, 223, 49, 41);
    contentPane.add(btnC);

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="+";
            s = tot.getText();
            tot.setText(s +"+");



        }
    });
    plus.setBounds(188, 67, 53, 41);
    contentPane.add(plus);

    JButton calc = new JButton("CALCULER");
    calc.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if (operateur == "+") {
            Total = Double.valueOf(Value) + Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));

        }
        else if (operateur=="-") {
            Total = Double.valueOf(Value) - Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));
        }
        else if (operateur=="*") {
            Total = Double.valueOf(Value) * Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));
        }
        else if (operateur=="/") {
            Total = Double.valueOf(Value) / Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));
        }
            Value = String.valueOf(Total);
            Value2 = "";
            operateur = "";
            Total = 0.0;
        }
    });
    calc.setBounds(10, 273, 231, 38);
    contentPane.add(calc);

    JButton moins = new JButton("-");
    moins.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="-";
            s = tot.getText();
            tot.setText(s+"-");


        }
    });
    moins.setBounds(189, 119, 52, 41);
    contentPane.add(moins);

    JButton fois = new JButton("*");
    fois.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="*";
            s = tot.getText();
            tot.setText(s+"*");



        }
    });
    fois.setBounds(188, 173, 53, 39);
    contentPane.add(fois);

    JButton sur = new JButton("/");
    sur.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="/";
            s = tot.getText();
            tot.setText(s+"/");
        }
    });
    sur.setBounds(189, 221, 52, 41);
    contentPane.add(sur);
}
Run Code Online (Sandbox Code Playgroud)

}

Mad*_*mer 5

所以,你有一大堆问题可能会回来咬你,但让我们看看眼前的问题......

public void actionPerformed(ActionEvent e) {
    if (operateur == "") { 
        Value = Value.trim() + 6.0;
    } else {
        Value2 = Value2.trim() + 6.0;
    }
    s = tot.getText();
    tot.setText(s+"6");
}
Run Code Online (Sandbox Code Playgroud)

每次按下时6,它都会附加6.0到结尾String,所以当你输入时1.66,你实际上正在1.6.06.0......这显然不是你想要的.

更改+ 6.0+ 6...和做同样的+ 3.0...

另请参阅如何比较Java中的字符串?Java TM编程语言的代码约定,它将使人们更容易阅读您的代码并让您阅读其他代码