gpt4 book ai didi

java - 柴笔 JAVA "AWT-EventQueue-0"java.lang.NumberFormatException : multiple points

转载 作者:行者123 更新时间:2023-11-29 07:45:36 27 4
gpt4 key购买 nike

我是第一次练习JAVA的学生。我们被要求创建一个简单的计算器。我所做的一切都没有任何错误。我唯一的问题是,当我执行 1.66 + 55 或什至 5 + 6 时,它会显示标题中的错误。这是代码

import java.awt.BorderLayout;

公共(public)类 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);
}

最佳答案

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

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");
}

每次您按下 6 时,它都会将 6.0 附加到 String 的末尾,因此当您键入 1.66,您实际上得到的是 1.6.06.0...这显然不是您想要的。

+ 6.0 更改为 + 6...并对 + 3.0 执行相同的操作...

另请查看 How do I compare strings in Java?Code Conventions for the Java TM Programming Language ,它会让人们更容易阅读你的代码,也让你更容易阅读别人的代码

关于java - 柴笔 JAVA "AWT-EventQueue-0"java.lang.NumberFormatException : multiple points,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26151811/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com