作业帮 > 综合 > 作业

我这个java计算器为啥只能输入一位数?

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/11/08 17:01:10
我这个java计算器为啥只能输入一位数?
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
public class NewCalculator extends WindowAdapter { JPanel j1 = new JPanel();
JPanel j2 = new JPanel();

JTextField jt = new JTextField(3);
private JButton[] b = new JButton[17];
private String s1[] = {"1","2","3","+","4","5","6","-","7","8","9","*","c","0","=","/"};
static double a;
static String s,str;

public static void main(String[] args) {
(new NewCalculator()).jframe();

} public void jframe(){
JFrame jf = new JFrame("计算器");
for(int i = 0 ; i < 16 ; i++){
b[i] = new JButton(s1[i]);
b[i].setPreferredSize(new Dimension(41,50));
}
for(int i = 0 ; i < 16 ; i++){
j2.add(b[i]);
}

jt.setEditable(false);
jt.setHorizontalAlignment(JTextField.RIGHT);

for(int i = 0 ; i < 16 ; i++){
b[i].addActionListener(new buttonlistener());
}

jf.addWindowListener(this);
jf.setBackground(Color.red);
j1.setLayout(new BorderLayout());
j1.add(jt,"North");
j2.setLayout(new FlowLayout(5,5,5));




jf.add(j1,"North");
jf.add(j2,"Center");
jf.setSize(195, 280);
jf.setVisible(true);
jf.setLocation(200, 200);
jf.setResizable(false);



}
class buttonlistener implements ActionListener{

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("=")){
jisuan();
str = String.valueOf(a);
jt.setText(str);
s= "";

}else if(e.getActionCommand().equals("+")){
jisuan();
jt.setText("+");
s = "+";

}else if(e.getActionCommand().equals("-")){
jisuan();
jt.setText("-");
s = "-";

}else if(e.getActionCommand().equals("*")){
jisuan();
jt.setText("*");
s = "*";

}else if(e.getActionCommand().equals("/")){
jisuan();
jt.setText("/");
s = "/";

}else if(e.getActionCommand().equals("c")){

jt.setText("");

}

else {
str = String.valueOf(e.getActionCommand());
jt.setText(str);
}
}
public void jisuan(){
if (s == "+") a+=Double.parseDouble(jt.getText());
else if (s == "-") a-=Double.parseDouble(jt.getText());
else if (s == "*") a*=Double.parseDouble(jt.getText());
else if (s == "/") a/=Double.parseDouble(jt.getText());
else a =Double.parseDouble(jt.getText());
}


}

}
你的程序我帮你改完了,你看看吧,改动的地方见注释.import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NewCalculator extends WindowAdapter {
JPanel j1 = new JPanel();
JPanel j2 = new JPanel();
JTextField jt = new JTextField(3);
private JButton[] b = new JButton[17];
private String s1[] = {"1","2","3","+","4","5","6","-","7","8","9","*","c","0","=","/"};
static double a;
static String s="",str="";//这里初始化变量s和str
public static void main(String[] args) {
(new NewCalculator()).jframe();
} public void jframe(){
JFrame jf = new JFrame("计算器");
for(int i = 0 ; i < 16 ; i++){
b[i] = new JButton(s1[i]);
b[i].setPreferredSize(new Dimension(41,50));
}
for(int i = 0 ; i < 16 ; i++){
j2.add(b[i]);
}
jt.setEditable(false);
jt.setHorizontalAlignment(JTextField.RIGHT);
for(int i = 0 ; i < 16 ; i++){
b[i].addActionListener(new buttonlistener());
}
jf.addWindowListener(this);
jf.setBackground(Color.red);
j1.setLayout(new BorderLayout());
j1.add(jt,"North");
j2.setLayout(new FlowLayout(5,5,5));
jf.add(j1,"North");
jf.add(j2,"Center");
jf.setSize(195, 280);
jf.setVisible(true);
jf.setLocation(200, 200);
jf.setResizable(false);
}
class buttonlistener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("=")){
jisuan();
str = String.valueOf(a);
jt.setText(str);
s= "=";//这里改一句 s="";改成s="=";
}else if(e.getActionCommand().equals("+")){
str="";//这里加一句
jisuan();
jt.setText("+");
s = "+";
}else if(e.getActionCommand().equals("-")){
str="";//这里加一句
jisuan();
jt.setText("-");
s = "-";
}else if(e.getActionCommand().equals("*")){
str="";//这里加一句
jisuan();
jt.setText("*");
s = "*";
}else if(e.getActionCommand().equals("/")){
str="";//这里加一句
jisuan();
jt.setText("/");
s = "/";
}else if(e.getActionCommand().equals("c")){
str="";//这里加一句
jt.setText("");
}
else {
if(s.equals("=")){//这里加if判断
str=String.valueOf(e.getActionCommand());//这里加一句
jt.setText(str);//这里加一句
s="";//这里加一句
}else{
str += String.valueOf(e.getActionCommand());//这里改一句str = String.valueOf(e.getActionCommand());改成str += String.valueOf(e.getActionCommand());
jt.setText(str);
}
}
}
public void jisuan(){
if (s.equals("+")) a+=Double.parseDouble(jt.getText()); //这里改一句 if判断s=="+"改成s.equals("+")
else if (s.equals("-")) a-=Double.parseDouble(jt.getText()); //这里改一句 if判断s=="-"改成s.equals("-")
else if (s.equals("*")) a*=Double.parseDouble(jt.getText()); //这里改一句 if判断s=="*"改成s.equals("*")
else if (s.equals("/")) a/=Double.parseDouble(jt.getText()); //这里改一句 if判断s=="/"改成s.equals("/")
else a =Double.parseDouble(jt.getText());
}
}
}