我这个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)

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/05 21:49:59
我这个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)
xWnF Hɉs45H!Z\9RJRv@^l.^6E @j Ϙ]^W$Rpj{Mrgvpf{?O^ >9ޓ}oWoNf7|v"UWU3dTg0-vta3kوS] ϗ`P=&KN np8%8PGN=Pt_鹮e[DKz0S;Xu695n5&ig`L8 c9a\q C뎋]`J18B6XTGLM_b{)5_lgkK%ݲq PFCu ![YBk t*uαZGEe .Ffw&-bDCCL’LY/B{Zź./%Qד@,16MZ]61W#O7FK 3ta7LJ؜%NN ]l1 o6q7?]sa*ԘMs9։Lzζ[nAvG-Zn?L~Kb("kid,Ėvq/TdSw=2 i51^VaiupmЇu ^ׇ w&Б<-BCt_whDkmD]&0gu:^rI%grR)8ԦNr|!~vQW#dPO xt+HfUcݚV&P[R­LURĸZ\-Mp3h&rњH«u i xXBO5ŶC{HW`#`%0P*Per750x_^!kֽ47ۃm.LWoLtGlzԽ/{NƖL($4@Q}@B0 .oij!dCXAȿ6o{;{/6v|mhk .6-=Al`h/r($Y *2~ H]G⬲>x\{_sDЊ=Tak߈3 |/S#mH!x)!m=2wQ3qeۃ)xP=@9)xh

我这个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)
我这个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());
}


}

}

我这个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)

你的程序我帮你改完了,你看看吧,改动的地方见注释.

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());
}
}
}

我这个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) 这个算式能用科学计算器输入吗? 在java中,定义一个字符串变量,当从键盘输入y或,继续循环或结束循环,如何要让这个变量只能输入y或者n, java 输入一个数,判断这个数是由哪两个质数相加组成JAVA 请问“直径”的符号如何输入我用紫光拼音只能输入这个符号“∮”.这个应该不是直径的符号. Java编写计算器的流程图 输入两个数,然后输入一个运算符,计算出结果输出,这个JAVA程序怎么写 手机三角函数计算器我的手机可以计算三角函数,但为啥按那个sin屏幕就会显示“sin(”,然后我输入sin(30,里面显示等于-0.988031624,这是怎么回事,要怎么调吗还是要怎么办? 我用java做了一个计算器,但做3.1-3=0.1000000000009.为什么? 计算器怎么输入立方 JAVA输入三角形的三条边,判断是否是一个三角形,三角形的形状JAVA语言实现一下这个题目:输入三角形的三条边判断是否等边三角形,等腰三角形,不等边三角形; 计算器的原理,为啥能算出数来? 我学的是java 要求用户输入一个年份比如 1998,然后求出这一年是闰年还是平年我学的是java 老师给我们出了这个这样一个题要求用户输入一个年份比如 1998,然后求出这一年是闰年还是平年这个 TI nspire cas怎么算标准差.比如说我想算1 2 3 4 5这五个数的标准差,我在计算器里面输入stDevSamp()这个函数,我怎么把这五个数输入进去? 为什么我的cad输入数值输入不了只能输入x y轴坐标 计算器怎么输入度分秒 casio5800计算器 方括号怎样输入 计算器三角函数运算不对是什么原因(科学计算器)比如我直接输入Sin30 ,答案却是0.45……