我在 Java(小程序)中的简单二进制代码转换器有问题:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class dual_convert extends Applet implements ActionListener{
TextField dual;
TextField decimal;
Button dual_b;
Button decimal_b;
Label dual_l;
Label decimal_l;
int decimal_i;
String dual_s [];
int dual_i;
int decimal_e;
public void init() {
setLayout(null);
dual = new TextField();
dual.setBounds(90,10,100,25);
add(dual);
decimal = new TextField();
decimal.setBounds(90,40,100,25);
add(decimal);
decimal_l = new Label("Decimal:");
decimal_l.setBounds(10,40,80,25);
add(decimal_l);
dual_l = new Label("Binary:");
dual_l.setBounds(10,10,80,25);
add(dual_l);
dual_b = new Button ("Convert");
dual_b.setBounds(200,10,80,25);
add(dual_b);
dual_b.addActionListener(this);
decimal_b = new Button ("Convert");
decimal_b.setBounds(200,40,80,25);
add(decimal_b);
decimal_b.addActionListener(this);
}
public void actionPerformed (ActionEvent e) {
if (e.getSource()==decimal_b) {
decimal_i = Integer.parseInt(decimal.getText());
int ih = 0;
dual.setText("");
while (decimal_i>0) {
dual.setText((decimal_i % 2)+dual.getText());
decimal_i = decimal_i / 2;
ih++;
}
}
if (e.getSource()==dual_b) {
dual_s = dual.getText().split("");
dual_i = dual_s.length;
decimal.setText("");
for (int i=0;dual_i>0;dual_i--) {
decimal_e = decimal_e + (Integer.parseInt(dual_s[dual_i-1]) * 2^i);
i++;
}
decimal.setText(decimal_e+"");
}
}
}
它通过按 Convert Binary to Decimal 引发此异常:
线程“AWT-EventQueue-1”java.lang.NumberFormatException 中的异常:对于输入字符串:“”在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java: 504) 在 java.lang.Integer.parseInt(Integer.java:527) 在 dual_convert.actionPerformed(dual_convert.java:65) 在 java.awt.Button.processActionEvent(Button.java:409) 在 java.awt.Button。 processEvent(Button.java:377) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java :729) 在 java.awt.EventQueue.access$200(EventQueue.java:103) 在 java.awt.EventQueue$3.run(EventQueue.java:688) 在 java.awt.EventQueue$3.run(EventQueue.java:686) )在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:702) 在 java.awt.EventQueue$4.run(EventQueue.java:700) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76 ) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java:699) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 在 java.awt .EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 在 java.awt。EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
对于二进制-->十进制转换,我使用这个系统(还有其他系统吗?):
100101 = [ ( 1 ) × 25 ] + [ ( 0 ) × 24 ] + [ ( 0 ) × 23 ] + [ ( 1 ) × 22 ] + [ ( 0 ) × 21 ] + [ ( 1 ) × 20 ]
100101 = [ 1 × 32 ] + [ 0 × 16 ] + [ 0 × 8 ] + [ 1 × 4 ] + [ 0 × 2 ] + [ 1 × 1 ]
100101 = 3710