这个计算器不能连续运算...
我做的一个简单的计算器,但是不能连续运算,大家帮我看看。
import java.awt.*;
import java.awt.event.*;
public class Calculatorimplements ActionListener
{
Button bPoint,bEqual,bPlus,bMinus,bClear,bMulti,bDivision;
Button[] b=new Button[10];
boolean isFloat = false;
Panel panel1=new Panel();
Panel panel2=new Panel();
Panel panel3=new Panel();
String currentOp = new String("");
String preOp = new String("");
String foreText = new String("");
String backText = new String("");
TextField tfAnswer=new TextField(8);
Frame jsq=new Frame("计算器");
public void jsqjm (){
jsq.setSize(300,200);
jsq.setLocation(400,100);
jsq.setBackground(Color.PINK);
jsq.addWindowListener(new mc());
for(int i=9;i>=0;i--){
b[i]=new Button(Integer.toString(i));
panel2.add(b[i]);
b[i].addActionListener(this);
}
bPoint = new Button(".");
bEqual=new Button("=");
bEqual.setForeground(Color.red);
bClear = new Button("清除");
bClear.setForeground(Color.red);
bDivision = new Button("/");
bDivision.setForeground(Color.red);
bMulti = new Button("*");
bMulti.setForeground(Color.red);
bMinus = new Button("-");
bMinus.setForeground(Color.red);
bPlus = new Button("+");
bPlus.setForeground(Color.red);
jsq.setLayout(new FlowLayout(FlowLayout.CENTER,5,35));
panel1.setLayout(new FlowLayout());
panel2.setLayout(new GridLayout(4,3));
panel3.setLayout(new GridLayout(4,1));
jsq.add(panel1);
jsq.add(panel2);
jsq.add(panel3);
panel1.add(tfAnswer);
panel1.add(bClear);
bClear.addActionListener(this);
panel2.add(bPoint);
bPoint.addActionListener(this);
panel2.add(bEqual);
bEqual.addActionListener(this);
panel3.add(bPlus);
bPlus.addActionListener(this);
panel3.add(bMinus);
bMinus.addActionListener(this);
panel3.add(bMulti);
bMulti.addActionListener(this);
panel3.add(bDivision);
bDivision.addActionListener(this);
jsq.setVisible(true);
}
public static void main (String[] args)
{
Calculator ftest=new Calculator();
ftest.jsqjm();
}
public void actionPerformed(ActionEvent e)
{
String s=new String("");
s=(String)e.getActionCommand();
if((s!=".")&&(s!="清除")&&(s!="+")&&(s!="-")&&(s!="/")&&(s!="*")&&(s!="="))
{ doForeText(s); }
if(s==".")
{
if(foreText.equals(""))
{
isFloat=true;
foreText+="0.";
tfAnswer.setText(foreText);
}
else
{
if(!isFloat)
doForeText(s);
}
}
if(s=="清除")
{ doClear(); }
if((s=="+")||(s=="-")||(s=="/")||(s=="*"))
{
if(foreText!=" "){
currentOp=s;
doOperator();
}
else { preOp=s;}
}
if(s.equals("="))
{ doOperator();}
}
public void doOperator(){
double dFore=0,dBack=0,resultNum=0;
Double d;
if(preOp.equals(""))
{
backText=foreText;
foreText="";
tfAnswer.setText(backText);
}
else
{
dFore=(new Double(foreText)).doubleValue();
dFore=getNumberFromText();
dBack=(new Double(backText)).doubleValue();
foreText="";
backText=tfAnswer.getText();
if (preOp.equals("+")) {
resultNum = dFore + dBack;
d=new Double(resultNum);
tfAnswer.setText(d.toString());
backText=d.toString();
}
else if(preOp.equals("-")) {
resultNum = dBack - dFore ;
d=new Double(resultNum);
tfAnswer.setText(d.toString());
backText=d.toString();
}
else if(preOp.equals("*")) {
resultNum = dBack * dFore ;
d=new Double(resultNum);
tfAnswer.setText(d.toString());
backText=d.toString();
}
else if(preOp.equals("/"))
{
if (dFore==0){
tfAnswer.setText("除数不能为0");
return;
}
resultNum = dBack / dFore ;
d = new Double(resultNum);
tfAnswer.setText(d.toString());
backText=d.toString();
}
}
preOp=currentOp;
}
//从结果文本框中获取数字
private double getNumberFromText()
{
double result = 0;
try
{
result = Double.valueOf(tfAnswer.getText()).doubleValue();
}
catch (NumberFormatException e)
{}
return result;
}
public void doForeText(String s) {
foreText+=s;
tfAnswer.setText(foreText);
}
public void doBackText(String s){
backText=foreText;
foreText="";
tfAnswer.setText(backText);
}
public void doClear() {
currentOp="";
preOp="";
foreText="";
backText="";
isFloat=false;
tfAnswer.setText("");
}
}
class mc implements WindowListener
{
public void windowClosed(WindowEvent e){
System.exit(0);
}
public void windowActivated(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
}
[解决办法]
我刚学JAVA几个月,第一次到这里混,自己写过一个,可以给楼主借鉴一下,写得不好各位室外高人不要减小,有完整的注释
你的那个代码我运行了,一时不能全部看懂,不好帮你改。我给你我的这个。不懂的可以帮你解释。用到数据结构队列的知识,科学型的那边没有开发,点不动。
主类的名字是mm,只有一个文件,3个类
字数超过限制了,我要分几次发给你,第一次365行,共800多行,你接起来就行了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Queue_ch
{/* 数组模拟队列,字符队列 */
static char num[] = new char[1000];
static int rear, head;
public Queue_ch() {
for (int i = 0; i < 1000; i++)
num[i] = 0;
head = rear = 0;
}
public void add(char i)
{/* 入队函数 */
num[head] = i;
head++;
System.out.println("符号入队列 " + number());
}
public char moved()
{/* 出队函数 */
char t = num[rear];
rear++;
System.out.println("符号出队列 " + number());
return t;
}
public int number()
{/* 队内元素个数判断函数 */
return head - rear + 1 - 1;
}
public void restatr()
{/* 队列重置函数 */
head = rear = 0;
}
}
class Queue_dou
{/* 数组模拟队列,双精度队列 */
static double num[] = new double[1000];
static int rear, head;
public Queue_dou() {
for (int i = 0; i < 1000; i++)
num[i] = 0;
head = rear = 0;
}
public void add(double i)
{/* 入队函数 */
num[head] = i;
head++;
System.out.println("数字入队列 " + number());
}
public double moved()
{/* 出队函数 */
double t = num[rear];
rear++;
System.out.println("数字出队列 " + number());
return t;
}
public int number()
{/* 队内元素个数判断函数 */
return head - rear + 1 - 1;
}
public void restatr()
{/* 队列重置函数 */
head = rear = 0;
}
}
public class mm
{
/* 显示框内有无数字标志(无数字时为0,一运算符与一数字入队时为0) */
static int IsText = 0;
/* 创建菜单条 */
static MenuBar mba = new MenuBar();
/* 创建菜单 */
static Menu m_View = new Menu("查看");
static Menu m_Edit = new Menu("编辑");
static Menu m_Help = new Menu("帮助");
/* 创建m_View菜单项 */
static MenuItem m_View_1 = new MenuItem("标准型", new MenuShortcut(KeyEvent.VK_1));
static MenuItem m_View_2 = new MenuItem("科学型", new MenuShortcut(KeyEvent.VK_2));
static MenuItem m_View_3 = new MenuItem("程序员", new MenuShortcut(KeyEvent.VK_3));
static MenuItem m_View_4 = new MenuItem("统计信息", new MenuShortcut(KeyEvent.VK_T));
static MenuItem m_View_5 = new MenuItem("历史记录", new MenuShortcut(KeyEvent.VK_H));
/* 创建m_Edit菜单项 */
static MenuItem m_Edit_1 = new MenuItem("复制", new MenuShortcut(KeyEvent.VK_C));
static MenuItem m_Edit_2 = new MenuItem("粘贴", new MenuShortcut(KeyEvent.VK_V));
/* 创建m_Help菜单项 */
static MenuItem m_Help_1 = new MenuItem("查看帮助", new MenuShortcut(KeyEvent.VK_F1));
static MenuItem m_Help_2 = new MenuItem("Made by Qiumoon");
/* 定义输入框面板 */
static JPanel pan1 = new JPanel();
/* 定义功能按钮面板 */
static JPanel pan2 = new JPanel();
/* 定义数字、符号按钮面板 */
static JPanel pan3 = new JPanel();
/* 定义记忆面板 */
static JPanel pan4 = new JPanel();
/* 定义科学型面板 */
static JPanel pan5 = new JPanel();
/* 定义程序主框架 */
static JFrame frm = new JFrame("计算器");
/* 定义俩队列实例 */
static Queue_ch Qch = new Queue_ch();
static Queue_dou Qdou = new Queue_dou();
/* 定义输入框 */
static TextArea ta = new TextArea("", 1, 1, TextArea.SCROLLBARS_NONE);
/* 定义数字面板按钮 */
static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bo, ba, bs, bp, bd, be;
/* 定义功能面板按钮 */
static JButton clear, negtive, backspace, close;
/* 定义记忆面板按钮 */
static JButton MC, MR, MP, MS;
/* 定义科学型面板按钮 */
static JButton square_2, one_div, square, n_0, sin, cos, tan, csc, pi, mod, log, In, x_y,
x_sq_y, e, Exp;
/* 定义成员函数,用于计算 */
public static void count_and_display(double a, double b, char ch)
{
ta.setText("");
System.out.println("进入函数count");
switch (ch)
{
case '+':
ta.setText(a + b + "");/* 计算结果DOUBLE型转换为字符串型,下同 */
Qdou.add(a + b);
break;
case '-':
ta.setText(a - b + "");
Qdou.add(a - b);
break;
case '*':
ta.setText(a * b + "");
Qdou.add(a * b);
break;
case '/':
ta.setText(a / b + "");
Qdou.add(a / b);
break;
default:
ta.setText("default");
System.out.println("default");
break;
}
}
public static void main(String[] args)
{
/* 框架frm相关配置 */
frm.setVisible(true);
frm.setLayout(null);
frm.setBackground(new Color(7, 1, 84));
frm.setResizable(true);
frm.setMenuBar(mba);
/* 各组建大小、位置相关配置 */
ta.setSize(180, 20);
ta.selectAll();
frm.setBounds(350, 350, 206, 290);
/* frm添加各组建 */
frm.add(pan1);
frm.add(pan2);
frm.add(pan3);
frm.add(pan4);
frm.add(pan5);
/* 面板相关设置 */
pan1.add(ta);
pan1.setLayout(null);
pan1.setBounds(5, 5, 180, 20);
pan2.setLayout(new GridLayout(1, 3, 1, 1));
pan2.setBounds(5, 35, 180, 30);
pan2.setBackground(new Color(83, 70, 240));
pan3.setLayout(new GridLayout(4, 4, 1, 1));
pan3.setBounds(5, 70, 180, 155);
pan3.setBackground(new Color(83, 70, 240));
pan4.setLayout(new GridLayout(1, 4, 1, 1));
pan4.setBounds(200, 35, 200, 30);
pan4.setBackground(new Color(83, 70, 240));
pan5.setLayout(new GridLayout(4, 4, 1, 1));
pan5.setBounds(200, 70, 200, 155);
pan5.setBackground(new Color(83, 70, 240));
/* 菜单栏相关设置 */
mba.add(m_View);
mba.add(m_Edit);
mba.add(m_Help);
m_View.add(m_View_1);
m_View.add(m_View_2);
m_View.add(m_View_3);
m_View.addSeparator();
m_View.add(m_View_4);
m_View.add(m_View_5);
m_Edit.add(m_Edit_1);
m_Edit.add(m_Edit_2);
m_Help.add(m_Help_1);
m_Help.add(m_Help_2);
/* 创建数字按钮 */
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
bo = new JButton(".");
ba = new JButton("+");
bs = new JButton("-");
bp = new JButton("*");
bd = new JButton("÷");
be = new JButton("=");
ba.setForeground(Color.red);
bs.setForeground(Color.red);
bp.setForeground(Color.red);
be.setForeground(Color.red);
bd.setForeground(Color.red);
/* 创建功能按钮 */
clear = new JButton("CE");
close = new JButton("Close");
negtive = new JButton("±");
backspace = new JButton("←");
/* 创建记忆按钮 */
MC = new JButton("MC");
MR = new JButton("MR");
MP = new JButton("M+");
MS = new JButton("M+");
MC.setForeground(Color.red);
MR.setForeground(Color.red);
MP.setForeground(Color.red);
MS.setForeground(Color.red);
/* 创建科学按钮 */
square_2 = new JButton("x*x");
one_div = new JButton("1/x");
square = new JButton("√");
n_0 = new JButton("n!");
sin = new JButton("sin");
cos = new JButton("cos");
tan = new JButton("tan");
csc = new JButton("csc");
pi = new JButton("π");
mod = new JButton("%");
log = new JButton("log");
In = new JButton("In");
x_y = new JButton("x^y");
x_sq_y = new JButton("x√y");
e = new JButton("e");
Exp = new JButton("Exp");
/* 将按钮添加进面板pan2 */
pan2.add(clear);
pan2.add(negtive);
pan2.add(backspace);
/* 将按钮添加进面板pan3 */
pan3.add(be);
pan3.add(ba);
pan3.add(bs);
pan3.add(bp);
pan3.add(bd);
pan3.add(b1);
pan3.add(b2);
pan3.add(b3);
pan3.add(b4);
pan3.add(b5);
pan3.add(b6);
pan3.add(b7);
pan3.add(b8);
pan3.add(b9);
pan3.add(b0);
pan3.add(bo);
/* 将按钮添加进面板pan4 */
pan4.add(MC);
pan4.add(MR);
pan4.add(MP);
pan4.add(MS);
/* 将按钮添加进面板pan5 */
pan5.add(square_2);
pan5.add(one_div);
pan5.add(square);
pan5.add(n_0);
pan5.add(sin);
pan5.add(cos);
pan5.add(tan);
pan5.add(csc);
pan5.add(pi);
pan5.add(mod);
pan5.add(log);
pan5.add(In);
pan5.add(x_y);
pan5.add(x_sq_y);
pan5.add(e);
pan5.add(Exp);
/* 0到9以及小数点按纽监听 */
button_number_listener Num = new button_number_listener();
b0.addActionListener(Num);
b1.addActionListener(Num);
b2.addActionListener(Num);
b3.addActionListener(Num);
b4.addActionListener(Num);
b5.addActionListener(Num);
b6.addActionListener(Num);
b7.addActionListener(Num);
b8.addActionListener(Num);
b9.addActionListener(Num);
bo.addActionListener(Num);
/* 四则运算符按纽监听 */
button_opreator_listener opre = new button_opreator_listener();
ba.addActionListener(opre);
bs.addActionListener(opre);
bp.addActionListener(opre);
bd.addActionListener(opre);
/* 等号按纽监听 */
be.addActionListener(new button_equel_listener());
/* 其他按纽监听 */
button_others_listener Oth = new button_others_listener();
clear.addActionListener(Oth);
close.addActionListener(Oth);
negtive.addActionListener(Oth);
backspace.addActionListener(Oth);
/* 菜单项监听 */
MyMenuItem My = new MyMenuItem();
m_View_1.addActionListener(My);
m_View_2.addActionListener(My);
m_View_3.addActionListener(My);
m_View_4.addActionListener(My);
m_View_5.addActionListener(My);
m_Edit_1.addActionListener(My);
m_Edit_2.addActionListener(My);
m_Help_1.addActionListener(My);
m_Help_2.addActionListener(My);
/* 调整按钮标签的布局 */
MC.setMargin(new Insets(1, 1, 1, 1));
MR.setMargin(new Insets(1, 1, 1, 1));
MP.setMargin(new Insets(1, 1, 1, 1));
MS.setMargin(new Insets(1, 1, 1, 1));
square_2.setMargin(new Insets(1, 1, 1, 1));
one_div.setMargin(new Insets(1, 1, 1, 1));
sin.setMargin(new Insets(1, 1, 1, 1));
cos.setMargin(new Insets(1, 1, 1, 1));
tan.setMargin(new Insets(1, 1, 1, 1));
csc.setMargin(new Insets(1, 1, 1, 1));
log.setMargin(new Insets(1, 1, 1, 1));
x_y.setMargin(new Insets(1, 1, 1, 1));
x_sq_y.setMargin(new Insets(1, 1, 1, 1));
e.setMargin(new Insets(1, 1, 1, 1));
Exp.setMargin(new Insets(1, 1, 1, 1));
ba.setMargin(new Insets(1, 1, 1, 1));
bs.setMargin(new Insets(1, 1, 1, 1));
bp.setMargin(new Insets(1, 1, 1, 1));
bd.setMargin(new Insets(1, 1, 1, 1));
be.setMargin(new Insets(1, 1, 1, 1));
}
[解决办法]
说错了,共500+行,上次发到365行,这次接着完
/* 定义”数字”按钮动作类 */
static class button_number_listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{/* 第一个操作数入队时允许输入第二个操作数 */
if (IsText == 0 && Qch.number() == 1)
ta.setText("");
JButton but = (JButton) e.getSource();
if (but == b0) {
IsText = 1;
ta.append("0");/* 追加字符串,下同 */
System.out.println(ta.getText());
}
if (but == b1) {
IsText = 1;
ta.append("1");
System.out.println(ta.getText());
}
if (but == b2) {
IsText = 1;
ta.append("2");
System.out.println(ta.getText());
}
if (but == b3) {
IsText = 1;
ta.append("3");
System.out.println(ta.getText());
}
if (but == b4) {
IsText = 1;
ta.append("4");
System.out.println(ta.getText());
}
if (but == b5) {
IsText = 1;
ta.append("5");
System.out.println(ta.getText());
}
if (but == b6) {
IsText = 1;
ta.append("6");
System.out.println(ta.getText());
}
if (but == b7) {
IsText = 1;
ta.append("7");
System.out.println(ta.getText());
}
if (but == b8) {
IsText = 1;
ta.append("8");
System.out.println(ta.getText());
}
if (but == b9) {
IsText = 1;
ta.append("9");
System.out.println(ta.getText());
}
if (but == bo) {
if (IsText == 0) {
ta.setText("0.");
}
int flag = 0;
String str = ta.getText();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '.')
flag = 1;
}
if (flag == 0) {
ta.append(".");
System.out.println(ta.getText());
}
}
}
}
/* 定义”四则运算符”按钮动作类 */
static class button_opreator_listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{/* 当连续按两次操作码时无效,当无操作数时无效 */
if (IsText == 1 || (Qdou.number() != 1 && Qch.number() != 1 && IsText == 1)) {
JButton but = (JButton) e.getSource();
String str = ta.getText();
/* 字符串型转换为DOUBLE型,下同 */
double num = Double.parseDouble(str);
/* 立即截取字符串入队 */
Qdou.add(num);
/* 截取运算符入队 */
if (but == ba) {
Qch.add('+');
IsText = 0;
}
if (but == bs) {
Qch.add('-');
IsText = 0;
}
if (but == bp) {
Qch.add('*');
IsText = 0;
}
if (but == bd) {
Qch.add('/');
IsText = 0;
}
}
if (Qdou.number() == 2) {/* 当操作数达到两个时候自动运算并完成相关进出队操作 */
char ch = Qch.moved();
double a = Qdou.moved();
double b = Qdou.moved();
count_and_display(a, b, ch);
}
}
}
/* 定义”等号“按钮动作类 */
static class button_equel_listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{/* 既无操作数也无操作码时无效,连续按两次等号无效 */
if (IsText == 1 && Qdou.number() == 1 && Qch.number() == 1) {
String str = ta.getText();
double num = Double.parseDouble(str);
Qdou.add(num);
char ch = Qch.moved();
double a = Qdou.moved();
double b = Qdou.moved();
count_and_display(a, b, ch);
Qch.restatr();
Qdou.restatr();
}
}
}
/* 定义”其他按键”按钮动作类 */
static class button_others_listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton but = (JButton) e.getSource();
if (but == clear) {
ta.setText("");
Qch.restatr();
Qdou.restatr();
IsText = 0;
}
if (but == backspace) {
String str = ta.getText();
int len = str.length();
ta.setText(str.substring(0, len - 1));
}
if (but == negtive) {/* 输入框内无数字时无效,连续按两次负号为正号 */
String str = ta.getText();
if (str.charAt(0) == '-')
ta.setText(str.substring(1));
else {
ta.setText("-");
ta.append(str);
}
}
if (but == close) {/* 关闭计算器 */
frm.dispose();
}
}
}
static class MyMenuItem implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
MenuItem it = (MenuItem) e.getSource();
if (it == m_View_1) {
frm.setSize(206, 290);
pan1.setSize(180, 20);
ta.setSize(180, 20);
}
if (it == m_View_2) {
frm.setSize(425, 290);
pan1.setSize(460, 20);
ta.setSize(395, 20);
}
if (it == m_View_3)
;
if (it == m_View_4)
;
if (it == m_View_5)
;
if (it == m_Edit_1)
;
if (it == m_Edit_2)
;
if (it == m_Help_1)
;
if (it == m_Help_2)
;
}
}
}