我的Java(applet)中的简单二进制代码转换器出现了问题:
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]) * 2^i);
i++;
}
decimal.setText(decimal_e+"");
}
}
}
通过按Convert Binary to Decimal:
抛出此异常线程“AWT-EventQueue-1”中的异常 java.lang.ArrayIndexOutOfBoundsException:8 at dual.actionPerformed(dual.java:65)at java.awt.Button.processActionEvent(Button.java:409)at 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)at at java.awt.EventQueue.access $ 200(EventQueue.java:103)at java.awt.EventQueue $ 3.run(EventQueue.java:688)at java.awt.EventQueue $ 3.run(EventQueue.java:686)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87) 在java.awt.EventQueue $ 4.run(EventQueue.java:702)at java.awt.EventQueue $ 4.run(EventQueue.java:700)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:699)at 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) at 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
https://en.wikipedia.org/wiki/Binary_number
编辑:Talas发布后有一个新的例外:
线程“AWT-EventQueue-1”中的异常 java.lang.NumberFormatException:对于输入字符串:“”at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在java.lang.Integer.parseInt(Integer.java:504)at java.lang.Integer.parseInt(Integer.java:527)at dual_convert.actionPerformed(dual_convert.java:64)at java.awt.Button.processActionEvent(Button.java:409)at 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)at at java.awt.EventQueue.access $ 200(EventQueue.java:103)at java.awt.EventQueue $ 3.run(EventQueue.java:688)at java.awt.EventQueue $ 3.run(EventQueue.java:686)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87) 在java.awt.EventQueue $ 4.run(EventQueue.java:702)at java.awt.EventQueue $ 4.run(EventQueue.java:700)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:699)at 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) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:0)
你有这个类和一个名为dual的文本域。我对这个编译感到有些惊讶,但这可能就是问题所在。
答案 1 :(得分:0)
问题在于:
for (int i=0;dual_i>0;dual_i--) {
decimal_e = decimal_e + (Integer.parseInt(dual_s[dual_i - 1]) * 2^i);
i++;
}
您应该使用dual_s[dual_i - 1]
代替dual_s[dual_i]
,因为dual_i = dual_s.length;
和数组索引以0
开头