当前位置: 代码迷 >> Java相关 >> 关于JAVA编写的计算器
  详细解决方案

关于JAVA编写的计算器

热度:193   发布时间:2007-10-19 03:23:12.0
关于JAVA编写的计算器

各位大师..昨天编写了一个只有加减的计算器..可是问题就一大堆..当我PRESS加号或者减号的时候.之前输入的数字就会全部清零.而且加减算法上也有问题.能帮我解决一下吗??我研究了很久都没研究出来...谢谢


import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;


public class calc extends Applet implements ActionListener
{ //declare global variables and constants
private JTextField displayBox;
private Button[] numButtons;
private String[] num = {"7","8","9","4","5","6","1","2","3","0","","C","+","-","="};
private int[] indexSorter = {7,8,9,4,5,6,1,2,3,0,10,11,12,13,14};
private long total=0;
private long subTotal=0;
private long numIdx=0;
private String numString="0";
private boolean firstNo = true;


//start method of applet
public void init()
{ GridBagLayout gLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gLayout);
this.setBackground(new Color(0,100,150));
this.setForeground(Color.black);

//text box for calculator display
Panel p = new Panel();
p.setLayout(new FlowLayout());
displayBox = new JTextField(15);
displayBox.setHorizontalAlignment(JTextField.RIGHT);
displayBox.setEditable(false);
displayBox.addActionListener(this);
p.add(displayBox);

//set gridBag constraints for this component (panel)
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(10,10,10,10);
c.anchor = GridBagConstraints.NORTH;

gLayout.setConstraints(p,c);
add(p);

//buttons for numbers
Panel q = new Panel();
q.setLayout(new GridLayout(5,3,20,20));
numButtons = new Button[num.length];

for (int i=0;i<num.length;i++)
{ numButtons[indexSorter[i]] = new Button(num[i]);
numButtons[indexSorter[i]].addActionListener(this);
q.add(numButtons[indexSorter[i]]);
}

c.gridheight = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.CENTER;
c.weighty = 1.0;

gLayout.setConstraints(q,c);
add(q);

}

//actions performed when button input recognised by ActionListener
public void actionPerformed(ActionEvent e)
{ Object source = e.getSource();
for (int i=0;i<10;i++)
{ if (source == numButtons[i])
{ numIdx=i;
if (firstNo)
{ numString="";
firstNo=false;
}
numString=numString+Long.toString(numIdx);
displayBox.setText(numString);
subTotal=(subTotal*10)+numIdx;
}
}

//clear
if (source == numButtons[11])
{ numString="0";
subTotal=0;
total=0;
displayBox.setText(numString);
firstNo=true;
}

//+
if (source == numButtons[12])
{
total=total+subTotal;
subTotal=0;
numString=Long.toString(0);
displayBox.setText(numString);
firstNo=true;
}

//-
if (source == numButtons[13])
{ total=total-subTotal;
subTotal=0;
numString=Long.toString(0);
displayBox.setText(numString);
firstNo=true;
}

//=
if (source == numButtons[14])
{ numString=Long.toString(total);
displayBox.setText(numString);
firstNo=true;
}
}
}

搜索更多相关的解决方案: JAVA  计算器  import  java  awt  

----------------解决方案--------------------------------------------------------
回复:(beiny)关于JAVA编写的计算器
感觉楼主的程序好像不大正确,就是计算那一部分,我试了一下,你的计算结果整个就是有一个推后,就是你按第二次加号是计算的第一次的结果。
建议你再定义两个操作数和一个操作符,这样按下+ - * / 的同时记录下第一各操作数和操作符,按下 = 时记录第二个操作数,并根据操作符来决定进行何种运算,同时把运算结果赋给第一个操作数,实现连续运算。
long num1,num2;
char op;
public void actionPerformed(ActionEvent e)
{
......
if (source == numButtons[12])
{
num1 = Integer.parseInt(numString);
op = '+';
firstNo=true;
}
......
if (source == numButtons[14])
{
num2 = Integer.parseInt(numString);

if(op == '+')
num1 = num1 + num2;
else
num1 = num1 - num2;
numString=Long.toString(num1);
displayBox.setText(numString);
firstNo=true;
}


}
----------------解决方案--------------------------------------------------------

哦.原来是这样..恍然大悟...谢谢~~大师..因为才开始学习JAVA.学的一知半解的...多谢多谢...以后要多努力多练习~


----------------解决方案--------------------------------------------------------
为什么按照楼上大师说的方法重新定义了还是不行..???
----------------解决方案--------------------------------------------------------
回复:(beiny)[em08][em08][em08]为什么按照楼上大师...

我试了一下,可以的

import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;


public class Test extends Applet implements ActionListener
{ //declare global variables and constants
private JTextField displayBox;
private Button[] numButtons;
private String[] num = {"7","8","9","4","5","6","1","2","3","0","","C","+","-","="};
private int[] indexSorter = {7,8,9,4,5,6,1,2,3,0,10,11,12,13,14};
private long num1=0;
private long num2=0;
private char op;
private long numIdx=0;
private String numString="0";
private boolean firstNo = true;


//start method of applet
public void init()
{ GridBagLayout gLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gLayout);
this.setBackground(new Color(0,100,150));
this.setForeground(Color.black);

//text box for calculator display
Panel p = new Panel();
p.setLayout(new FlowLayout());
displayBox = new JTextField(15);
displayBox.setHorizontalAlignment(JTextField.RIGHT);
displayBox.setEditable(false);
displayBox.addActionListener(this);
p.add(displayBox);

//set gridBag constraints for this component (panel)
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(10,10,10,10);
c.anchor = GridBagConstraints.NORTH;

gLayout.setConstraints(p,c);
add(p);

//buttons for numbers
Panel q = new Panel();
q.setLayout(new GridLayout(5,3,20,20));
numButtons = new Button[num.length];

for (int i=0;i<num.length;i++)
{ numButtons[indexSorter[i]] = new Button(num[i]);
numButtons[indexSorter[i]].addActionListener(this);
q.add(numButtons[indexSorter[i]]);
}

c.gridheight = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.CENTER;
c.weighty = 1.0;

gLayout.setConstraints(q,c);
add(q);

}

//actions performed when button input recognised by ActionListener
public void actionPerformed(ActionEvent e)
{ Object source = e.getSource();
for (int i=0;i<10;i++)
{ if (source == numButtons[i])
{ numIdx=i;
if (firstNo)
{ numString="";
firstNo=false;
}
numString=numString+Long.toString(numIdx);
displayBox.setText(numString);
// subTotal=(subTotal*10)+numIdx;
}
}

//clear
if (source == numButtons[11])
{ numString="0";
num1=0;
num2=0;
displayBox.setText(numString);
firstNo=true;
}

//+
if (source == numButtons[12])
{
num1=Long.parseLong(numString);
op = '+';
firstNo=true;
}

//-
if (source == numButtons[13])
{
num1=Long.parseLong(numString);
op = '-';
firstNo=true;
}

//=
if (source == numButtons[14])
{
num2 = Long.parseLong(numString);

if(op == '+')
num1 = num1 + num2;
else
num1 = num1 - num2;
numString=Long.toString(num1);
displayBox.setText(numString);
firstNo=true;

}
}
}


----------------解决方案--------------------------------------------------------

无法实现混加混减功能


----------------解决方案--------------------------------------------------------
  相关解决方案