当前位置: 代码迷 >> J2SE >> 计算器,该如何解决
  详细解决方案

计算器,该如何解决

热度:376   发布时间:2016-04-24 01:42:51.0
计算器
import java.awt.*;
import java.awt.event.*;

public class MyCalculator {
public static void main(String[] args) throws Exception {
new MyFrame("计算器").launchFrame();
}
}

class MyFrame extends Frame {

private static final String s = "789*456/123-.0=+";
Panel p1 = new Panel();
Panel p2 = new Panel(new GridLayout(1,4));
Panel p3 = new Panel(new GridLayout(4,4));
TextField tf = new TextField(40);
Button[] btn = new Button[20];
Button reset= new Button("reset");
Button bt = new Button("");
StringBuffer formula = new StringBuffer();
String sformula = new String();
//ArrayList<String> formula = new ArrayList<String>();

public MyFrame(String s){
super(s);
setLocation(300,300);
setSize(400,550);
}

public void launchFrame() throws Exception {
setSize(400,650);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.SOUTH);
p1.add(tf);
p2.add(reset);
setVisible(true);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(-1);
}
});

for(int i = 0; i<s.length(); i++) {
btn[i] = new Button(s.substring(i, i+1));
btn[i].addActionListener(new MyButtonMonitor());
p3.add(btn[i]);
}

btn[14].addActionListener(new MyEqualsMonitor());

pack();

reset.addActionListener(new MyResetMonitor());

}

class MyResetMonitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf.setText("");
formula = new StringBuffer();
}
}

class MyButtonMonitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
formula.append(e.getActionCommand());
//System.out.println(formula.toArray());
sformula = formula.toString();
tf.setText(sformula);
}
}

class MyEqualsMonitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
for(int i = 0; i<sformula.length(); i++) {
btn[i] = new Button(s.substring(i, i+1));
btn[i].addActionListener(new MyButtonMonitor());
p3.add(btn[i]);
}
int i = 0;
char c;
double first = 0;
double last = 0;
double answer = 0;
do {
c = sformula.charAt(i);
i++;
} while( (c >= 0 && c <= 9) || c == 46);

first = Double.parseDouble(sformula.substring(0, i--));
last = Double .parseDouble(sformula.substring(i++, sformula.length()-1));
//answer = first + last;
if(sformula.substring(i,i++) == "+") {
answer = first + last;
} else if (c == '-') {
answer = first - last;
} else if (c == '*') {
answer = first*last;
} else if(c == '/') {
answer = first/last;
}
System.out.println(answer);
}
}

}

错误:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1.2-1.0"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:510)
at MyFrame$MyEqualsMonitor.actionPerformed(MyCalculator.java:93)
at java.awt.AWTEventMulticaster.actionPerformed(AWTEventMulticaster.java:386)
at java.awt.Button.processActionEvent(Button.java:388)
at java.awt.Button.processEvent(Button.java:356)
at java.awt.Component.dispatchEventImpl(Component.java:4407)
at java.awt.Component.dispatchEvent(Component.java:4237)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:600)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
  相关解决方案