没做完的计算器,ActionListener下面的if语句不跑啊 求解释啊。。。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class UseArithmetic extends Applet {
Label prompt,result;
TextField input1,input2,input3;
Button btn0,btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13,btn14;
int a=2, b=1,d=3;
String c="";
public void init( )
{
result=new Label("");
prompt = new Label("输入");
input1 = new TextField(5);
input2 = new TextField(5);
input3 = new TextField(5);
btn0= new Button("0");
btn1= new Button("1");
btn2= new Button("2");
btn3= new Button("3");
btn4= new Button("4");
btn5= new Button("5");
btn6= new Button("6");
btn7= new Button("7");
btn8= new Button("8");
btn9= new Button("9");
btn10= new Button("+");
btn11= new Button("-");
btn12= new Button("*");
btn13= new Button("/");
btn14=new Button("=");
add(result);
add(prompt);
add(input1);
add(input2);
add(input3);
add(btn0);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
add(btn5);
add(btn6);
add(btn7);
add(btn8);
add(btn9);
add(btn10);
add(btn11);
add(btn12);
add(btn13);
add(btn14);
btn0.addActionListener(new BtnActionAdapter());
btn1.addActionListener(new BtnActionAdapter());
btn2.addActionListener(new BtnActionAdapter());
btn3.addActionListener(new BtnActionAdapter());
btn4.addActionListener(new BtnActionAdapter());
btn5.addActionListener(new BtnActionAdapter());
btn6.addActionListener(new BtnActionAdapter());
btn7.addActionListener(new BtnActionAdapter());
btn8.addActionListener(new BtnActionAdapter());
btn9.addActionListener(new BtnActionAdapter());
btn10.addActionListener(new fuhaoActionAdapter());
btn11.addActionListener(new fuhaoActionAdapter());
btn12.addActionListener(new fuhaoActionAdapter());
btn13.addActionListener(new fuhaoActionAdapter());
btn14.addActionListener(new denghaoActionAdapter());
}
/* public void paint(Graphics g)
{
g.drawString("结果是:"+d+a+b+c, 10, 130);
}
*/
class BtnActionAdapter implements ActionListener{
public void actionPerformed(ActionEvent e)
{
input1.setText(((Button)e.getSource()).getLabel());
input3.setText(((Button)e.getSource()).getLabel());
a = Integer.parseInt(input1.getText());
b= Integer.parseInt(input3.getText());
}
}
class fuhaoActionAdapter implements ActionListener{
public void actionPerformed(ActionEvent e)
{
input2.setText(((Button)e.getSource()).getLabel());
c=input2.getText();
}
}
class denghaoActionAdapter implements ActionListener{
public void actionPerformed(ActionEvent e)
{
int d=3;
if(c=="+") //这里它不跑
d=a+b;
else if(c=="-")
d=a-b;
else if(c=="*")
d=a*b;
else if(c=="/")
d=a/b;
result.setText(a+c+b+"="+d);
}
}
}
[解决办法]
(c=="+") //这里它不跑,是什么意思?程序卡到这里?还有(c=="+") 改为("+".equals(c))
[解决办法]
你也稍微把这些控件布局一下呀。。乱的一锅粥。。
- Java code
import java.applet.*;import java.awt.*;import java.awt.event.*;public class UseArithmetic extends Applet { Label prompt, result; TextField input1, input2, input3; Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14; int a = 2, b = 1, d = 3; String c = ""; public void init() { result = new Label(""); prompt = new Label("输入"); input1 = new TextField(5); input2 = new TextField(5); input3 = new TextField(5); btn0 = new Button("0"); btn1 = new Button("1"); btn2 = new Button("2"); btn3 = new Button("3"); btn4 = new Button("4"); btn5 = new Button("5"); btn6 = new Button("6"); btn7 = new Button("7"); btn8 = new Button("8"); btn9 = new Button("9"); btn10 = new Button("+"); btn11 = new Button("-"); btn12 = new Button("*"); btn13 = new Button("/"); btn14 = new Button("="); add(result); add(prompt); add(input1); add(input2); add(input3); add(btn0); add(btn1); add(btn2); add(btn3); add(btn4); add(btn5); add(btn6); add(btn7); add(btn8); add(btn9); add(btn10); add(btn11); add(btn12); add(btn13); add(btn14); btn0.addActionListener(new BtnActionAdapter()); btn1.addActionListener(new BtnActionAdapter()); btn2.addActionListener(new BtnActionAdapter()); btn3.addActionListener(new BtnActionAdapter()); btn4.addActionListener(new BtnActionAdapter()); btn5.addActionListener(new BtnActionAdapter()); btn6.addActionListener(new BtnActionAdapter()); btn7.addActionListener(new BtnActionAdapter()); btn8.addActionListener(new BtnActionAdapter()); btn9.addActionListener(new BtnActionAdapter()); btn10.addActionListener(new fuhaoActionAdapter()); btn11.addActionListener(new fuhaoActionAdapter()); btn12.addActionListener(new fuhaoActionAdapter()); btn13.addActionListener(new fuhaoActionAdapter()); btn14.addActionListener(new denghaoActionAdapter()); } /* * public void paint(Graphics g) { * * g.drawString("结果是:"+d+a+b+c, 10, 130); } */ class BtnActionAdapter implements ActionListener { public void actionPerformed(ActionEvent e) { //这里要判断输入的数字是第一个运算数(a)还是第二个运算数(b) if(input2.getText().equals("")){ input1.setText(((Button) e.getSource()).getLabel()); a = Integer.parseInt(input1.getText()); } else { input3.setText(((Button) e.getSource()).getLabel()); b = Integer.parseInt(input3.getText()); } } } class fuhaoActionAdapter implements ActionListener { public void actionPerformed(ActionEvent e) { input2.setText(((Button) e.getSource()).getLabel()); c = input2.getText(); } } class denghaoActionAdapter implements ActionListener { public void actionPerformed(ActionEvent e) { int d = 3;// if (c == "+") // 这里它不跑// d = a + b;// else if (c == "-")// d = a - b;// else if (c == "*")// d = a * b;// else if (c == "/")// d = a / b; if (c.equals("+")) d = a + b; else if (c.equals("-")) d = a - b; else if (c.equals("*")) d = a * b; else if (c.equals("/")) d = a / b; result.setText(a + c + b + "=" + d); //等号执行以后,要将输入框中的内容清空,方便进行下一次运算 input1.setText(""); input2.setText(""); input3.setText(""); } }}
[解决办法]
字符串比较还是用equals()方法比较好
