我创建了这个类似于计算器的小代码,但只要有小数值,我就无法工作 有3个输入框1是名称第2是MorI它是英制或公制第3是高度以米或英寸
代码:
DecimalFormat decFor= new DecimalFormat("#.00");
String strName=Name.getText();
String strOr=MorI.getText();
int H=Integer.parseInt(height.getText());
System.out.println(H);
switch (strOr)
{
case "M" :
JOptionPane.showMessageDialog(null, strName+" ideal weight is "+(H*H*25/703)+"lbs");
System.out.println(strName+" ideal weight is "+(H*H*25/703)+"lbs");
break;
case "I" :
JOptionPane.showMessageDialog(null, strName+" ideal weight is "+ decFor.format(H*H*25) + "kgs");
System.out.println(strName+" ideal weight is "+ decFor.format(H*H*25) + "kgs");
break;
default: JOptionPane.showMessageDialog(null, "chose M or I nothing else");
错误看起来像
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1.6"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at U2_A8_A.Naseer.jButton1ActionPerformed(Naseer.java:147)
at U2_A8_A.Naseer.access$100(Naseer.java:16)
at U2_A8_A.Naseer$2.actionPerformed(Naseer.java:76)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:1)
你的身高很可能是一个浮点。
更改行:
int H=Integer.parseInt(height.getText());
成:
double h = Double.parseDouble(height.getText());