gpt4 book ai didi

java - 在计算器中按取消按钮 'C' 后出现错误(Eclipse IDE 和 swings)

转载 作者:行者123 更新时间:2023-12-02 03:26:27 27 4
gpt4 key购买 nike

这是我的取消按钮代码,当我在按“=”按钮之前按下它时,它工作正常,但是在显示最终结果时按“=”之后,然后如果按下“C”按钮,则它显示 java.lang.Cancel 按钮。这里的 lang.NumberFormatException 是一段代码,'convert'&'s'是一个字符串

    if(e.getSource() == buttonOp){
convert = Integer.parseInt(txt.getText());
System.out.println("parsing done!");
String choice = e.getActionCommand();
if(choice.equals("x^2")){
txt.setText("");
total = convert * convert;
System.out.println("total is"+total);
s += txt.getText();
}
else if(choice.equals("x^3")){
txt.setText("");
total = convert * convert * convert;
s += txt.getText();
}
else if (choice.equals("=")){
//txt.setText(choice);
// number = true;
number = false;
txt.setText("");
System.out.println("pressed '='");
if(txt.getText().length() > 0){
StringBuilder sb = new StringBuilder(txt.getText());
sb.delete(0, txt.getText().length());
s = sb.toString();
}
s = Double.toString(total);
s += txt.getText();
}
/*else if (choice.equals("+")){
total = convert;

}*/
else if(choice.equals("C")){
System.out.println("pressing C");
if(txt.getText().length()>0){
StringBuilder str = new StringBuilder(txt.getText());
str.deleteCharAt(txt.getText().length()-1);
s = str.toString();
}
txt.setText(s);
}
}
else{
txt.setText(null);
}
txt.setText(s);
}
});

这是我在“=”后按“Ç”得到的输出

    parsing done!
total is2601.0
parsing done!
pressed '='
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2601.0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at cal.Calculator$2.actionPerformed(Calculator.java:95)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

来自异常

....parseInt(Unknown Source)...

...For input string: "2601.0"...

因此,您正在为非 int 格式的值(“2601.0”)调用 parseInt()

我认为你的问题就在这里

if(e.getSource() == buttonOp){
convert = Integer.parseInt(txt.getText()); // <<<<

所以要么将其放在更合适的位置(在 if-else 内)或使用 Dobule.parseDouble() ,因为计算器应用程序很可能使用非 int 值。

关于java - 在计算器中按取消按钮 'C' 后出现错误(Eclipse IDE 和 swings),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38814126/

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