我用我的代码做了一个计算机,但是当我输入90,然后点击tan,再点击=时,结果是一串数字而不是error。每次值为0的时候,都会是一串数字,而不是0。
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
public class Calculator
{
public static void main(String[] args)
{
JFrame f = new JFrame("TCA version 3.02 ");
CalculatorPanel calc = new CalculatorPanel();
// set properties of the JFrame f;
f.setSize(400, 300);
f.getContentPane().add(calc);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false); // maximize option is disabled.
f.setVisible(true);
}
} // end of Calculator class
// Class CalculatorPanel class
class CalculatorPanel extends JPanel implements ActionListener
{
JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal,f1,f2,f3,f4,f5,f6,f7,f8,f9;
static JTextField result=new JTextField("0",45);
static String lastCommand=null;
JOptionPane p=new JOptionPane();
double preRes=0,secVal=0,res;
double preRes2=0;
// Unicode constants
final String UNICODE_BACKSPACE = "\u232B";
final String UNICODE_SQUAREROOT = "\u221A";
final String UNICODE_E = "\u0065";
final String UNICODE_PI = "\u03c0";
private static void assign(String no)
{
if((result.getText()).equals("0"))
result.setText(no);
else if(lastCommand=="=")
{
result.setText(no);
lastCommand=null;
}
else
result.setText(result.getText()+no);
}
public CalculatorPanel()
{
setLayout(new BorderLayout());
result.setEditable(false);
result.setSize(300,200);
add(result,BorderLayout.NORTH);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(5,5));
// add buttons 7, 8, 9, / (Divide)
n7=new JButton("7");
panel.add(n7);
n7.addActionListener(this);
n8=new JButton("8");
panel.add(n8);
n8.addActionListener(this);
n9=new JButton("9");
panel.add(n9);
n9.addActionListener(this);
div=new JButton("/");
panel.add(div);
div.addActionListener(this);
// create buttons f1, f2
f1=new JButton(UNICODE_BACKSPACE);
panel.add(f1);
f1.addActionListener(this);
// add buttons 4, 5, 6, * (Multiply)
n4=new JButton("4");
panel.add(n4);
n4.addActionListener(this);
n5=new JButton("5");
panel.add(n5);
n5.addActionListener(this);
n6=new JButton("6");
panel.add(n6);
n6.addActionListener(this);
mul=new JButton("*");
panel.add(mul);
mul.addActionListener(this);
f2=new JButton(UNICODE_SQUAREROOT);
panel.add(f2);
f2.addActionListener(this);
// add buttons 1, 2, 3, - (subtract)
n1=new JButton("1");
panel.add(n1);
n1.addActionListener(this);
n2=new JButton("2");
panel.add(n2);
n2.addActionListener(this);
n3=new JButton("3");
panel.add(n3);
n3.addActionListener(this);
minus=new JButton("-");
panel.add(minus);
minus.addActionListener(this);
f3=new JButton("x^2");
panel.add(f3);
f3.addActionListener(this);
// add buttons . (dot), 0, =, + (add)
dot=new JButton(".");
panel.add(dot);
dot.addActionListener(this);
n0=new JButton("0");
panel.add(n0);
n0.addActionListener(this);
equal=new JButton("=");
panel.add(equal);
equal.addActionListener(this);
plus=new JButton("+");
panel.add(plus);
plus.addActionListener(this);
f4=new JButton("C");
panel.add(f4);
f4.addActionListener(this);
f5=new JButton("sin");
panel.add(f5);
f5.addActionListener(this);
f6=new JButton("cos");
panel.add(f6);
f6.addActionListener(this);
f7=new JButton("tan");
panel.add(f7);
f7.addActionListener(this);
f8=new JButton(UNICODE_E);
panel.add(f8);
f8.addActionListener(this);
f9=new JButton(UNICODE_PI);
panel.add(f9);
f9.addActionListener(this);
add(panel,BorderLayout.CENTER);
} // add panel to JFrame
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==n1) assign("1");
else if(ae.getSource()==n2) assign("2");
else if(ae.getSource()==n3) assign("3");
else if(ae.getSource()==n4) assign("4");
else if(ae.getSource()==n5) assign("5");
else if(ae.getSource()==n6) assign("6");
else if(ae.getSource()==n7) assign("7");
else if(ae.getSource()==n8) assign("8");
else if(ae.getSource()==n9) assign("9");
else if(ae.getSource()==n0) assign("0");
else if(ae.getSource()==dot)
{
if(((result.getText()).indexOf("."))==-1)
result.setText(result.getText()+".");
}
else if(ae.getSource()==minus)
{
preRes=Double.parseDouble(result.getText());
lastCommand="-";
result.setText("0");
}
else if(ae.getSource()==div)
{
preRes=Double.parseDouble(result.getText());
lastCommand="/";
result.setText("0");
}
else if(ae.getSource()==equal)
{
secVal=Double.parseDouble(result.getText());
if (lastCommand.equals("/"))
res=preRes/secVal;
else if(lastCommand.equals("*"))
res=preRes*secVal;
else if(lastCommand.equals("-"))
res=preRes-secVal;
else if(lastCommand.equals("+"))
res=preRes+secVal;
else if(lastCommand.equals("sin"))
res=Math.sin(Math.toRadians(preRes));
else if(lastCommand.equals("cos"))
res=Math.cos(Math.toRadians(preRes));
else if(lastCommand.equals("tan"))
res=Math.tan(Math.toRadians(preRes));
else if(lastCommand.equals("C"))
res=0; secVal=0;
result.setText(" "+res);
lastCommand="=";
}
else if(ae.getSource()==mul)
{
preRes=Double.parseDouble(result.getText());
lastCommand="*";
result.setText("0");
}
else if(ae.getSource()==plus)
{
preRes=Double.parseDouble(result.getText());
lastCommand="+";
result.setText("0");
}
else if(ae.getSource()==f1)
{
// String strMessage = "Function f1 was pressed";
String strResult = result.getText();
String strNewResult = null;
int strNewLength = strResult.length() - 1;
if (strNewLength == 0) {
result.setText("0");
} else {
strNewResult = strResult.substring(0, strNewLength);
result.setText(strNewResult);
}
JOptionPane.showMessageDialog(null, strNewResult);
}
else if(ae.getSource()==f4)
{
preRes=Double.parseDouble("0");
lastCommand="C";
result.setText("0");
}
else if(ae.getSource()==f5)
{
preRes=Double.parseDouble(result.getText());
lastCommand="sin";
result.setText("0");
}
else if(ae.getSource()==f6)
{
preRes=Double.parseDouble(result.getText());
lastCommand="cos";
result.setText("0");
}
else if(ae.getSource()==f7)
{
preRes=Double.parseDouble(result.getText());
lastCommand="tan";
result.setText("0");
}
else if(ae.getSource()==f8)
{
result.setText(Double.toString(Math.E));
}
else if(ae.getSource()==f9)
{
result.setText(Double.toString(Math.PI));
}
// end cascading if...else if.
} // actionPerformed method
} // end of CalculatorPanel class