java 记录下使用计算器计算的步骤以及计算得出的结果,为什么不显示计算符号?

筱周 2010-02-08 08:44:21
计算器的代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class JiSuanQi {
public static void main(String[] args) {
DFrame frame = new DFrame();
frame.setTitle("计算器");
frame.setBackground(Color.lightGray);
frame.setSize(325, 325);
frame.setVisible(true);

}
}
class DFrame extends JFrame implements ActionListener {
JPanel p0, p1, p2, p3;// 定义面板
JTextField tf1;// 定义计算界面和结果显示区
TextField tf2; // 定义缓存区
String[] s = { "Backspace", "CE", "C", "MC", "MR", "MS", "M+", "7", "8",
"9", "/", "平方根", "4", "5", "6", "*", "平方", "1", "2", "3", "-",
"立方", "0", "+/-", ".", "+", "=" };
Button[] b = new Button[s.length];// 定义按钮
String copycontent = "";
StringBuffer str;
StringBuffer sb;
double x, y;// 定义计算数
int z;// 定义事件
static double m;// 定义缓存数

public DFrame() {
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu menu = new JMenu("编辑");
menuBar.add(menu);//编辑菜单

JMenuItem menuItemOne = new JMenuItem("复制");
menuItemOne.addActionListener(this);
menuItemOne.setActionCommand("copy");
JMenuItem menuItemTwo = new JMenuItem("粘贴");
menuItemTwo.addActionListener(this);
menuItemTwo.setActionCommand("paste");//定义复制和粘贴选项

menu.add(menuItemOne);
menu.add(menuItemTwo);

tf1 = new JTextField(27);
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setEnabled(false);
tf1.setText("0");
tf2 = new TextField(10);
tf2.setEditable(false);// 定义计算界面和结果显示区

for (int i = 0; i < s.length; i++) {
b[i] = new Button(s[i]);
b[i].addActionListener(this);
}//定义按钮

p0 = new JPanel();
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();//定义面板

str = new StringBuffer();
p0.add(tf1);
p0.setBounds(10, 25, 300, 40);//将计算界面和结果显示区添加到面板p0中,设置面板位置

p1.setLayout(new GridLayout(1, 4, 10, 0));//定义面板的布局方式
p1.add(tf2);
p1.add(b[0]);
p1.add(b[1]);
p1.add(b[2]);
p1.setBounds(10, 65, 300, 25);//添加按钮到p1,设置面板位置

p2.setLayout(new GridLayout(4, 1, 0, 15));
p2.add(b[3]);
p2.add(b[4]);
p2.add(b[5]);
p2.add(b[6]);
p2.setBounds(10, 110, 40, 150);//添加按钮到p2,设置面板位置

p3.setLayout(new GridLayout(4, 5, 10, 15));
for (int j = 7; j < 27; j++) {
p3.add(b[j]);
}
p3.setBounds(60, 110, 250, 150);//添加按钮到p3,设置面板位置

this.setLayout(null);
this.add(p0);
this.add(p1);
this.add(p2);
this.add(p3);
this.setResizable(false);//将面板添加到窗口中
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == b[1]) {
tf1.setText("0");
str.setLength(0);//清除
} else if (e.getSource() == b[2]) {
tf1.setText("0");
str.setLength(0);//清零
} else if (e.getSource() == b[23]) {
x = Double.parseDouble(tf1.getText().trim());
tf1.setText("" + (-x));//判断正负数
} else if (e.getSource() == b[25]) {
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y = 0d;
z = 0;//计算加法
} else if (e.getSource() == b[20]) {
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y = 0d;
z = 1;//计算减法
} else if (e.getSource() == b[15]) {
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y = 0d;
z = 2;//计算除法
} else if (e.getSource() == b[10]) {
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y = 0d;
z = 3;//计算乘法
} else if (e.getSource() == b[26]) {
str.setLength(0);
switch (z) {
case 0:
tf1.setText("" + (x + y));
break;
case 1:
tf1.setText("" + (x - y));
break;
case 2:
tf1.setText("" + (x * y));
break;
case 3:
if (Double.parseDouble(tf1.getText()) == 0) {
tf1.setText("除数不能为零");
} else {
tf1.setText("" + (x / y));
}
break;
}

} else if (e.getSource() == b[24]) {
if (tf1.getText().trim().indexOf('.') != -1) { //判断小数

} else {
if (tf1.getText().trim().equals("0")) {
str.setLength(0);
tf1.setText((str.append("0" + e.getActionCommand()))
.toString());
} else if (tf1.getText().trim().equals("")) {
} else {
tf1
.setText(str.append(e.getActionCommand())
.toString());
}
}

y = 0d;

} else if (e.getSource() == b[11]) {
x = Double.parseDouble(tf1.getText().trim());
tf1.setText("数字格式异常");
if (x < 0)
tf1.setText("负数没有平方根");
else
tf1.setText("" + Math.sqrt(x));//开根运算
str.setLength(0);
y = 0d;
} else if (e.getSource() == b[16]) {
x = Double.parseDouble(tf1.getText().trim());
tf1.setText("" + (x * x));//平方运算
str.setLength(0);
y = 0d;
} else if (e.getSource() == b[21]) {
x = Double.parseDouble(tf1.getText().trim());
tf1.setText("" + (x * x * x));//立方运算
str.setLength(0);
y = 0d;

} else if (e.getSource() == b[3]) {//缓存数操作
m = 0d;
tf2.setText("");
str.setLength(0);
} else if (e.getSource() == b[4]) {
if (tf2.getText().trim() != "") {
tf1.setText("" + m);
}
} else if (e.getSource() == b[5]) {

m = Double.parseDouble(tf1.getText().trim());
tf2.setText("M");
tf1.setText("0");
str.setLength(0);
} else if (e.getSource() == b[6]) {
m = m + Double.parseDouble(tf1.getText().trim());
} else {
if (e.getSource() == b[22]) {
if (tf1.getText().trim().equals("0")) {

} else {
tf1
.setText(str.append(e.getActionCommand())
.toString());
y = Double.parseDouble(tf1.getText().trim());
}
} else if (e.getSource() == b[0]) {//退后操作
if (!tf1.getText().trim().equals("0")) {
if (str.length() != 1) {
tf1.setText(str.delete(str.length() - 1,
str.length()).toString());
} else {
tf1.setText("0");
str.setLength(0);
}
}
y = Double.parseDouble(tf1.getText().trim());
} else if (!e.getActionCommand().equals("copy")
&& !e.getActionCommand().equals("paste")) {//复制和粘贴操作
tf1.setText(str.append(e.getActionCommand()).toString());
y = Double.parseDouble(tf1.getText().trim());
}
}
} catch (NumberFormatException e1) {
tf1.setText("数字格式异常");
} catch (StringIndexOutOfBoundsException e1) {
tf1.setText("字符串索引越界");
}
if (e.getActionCommand().equals("copy")) {
copycontent = tf1.getText();
}
if (e.getActionCommand().equals("paste")) {
tf1.setText(tf1.getText() + copycontent);
}
try{
StringBuffer sb = new StringBuffer();
sb.append(x);
if (e.getSource() == b[10]) {
sb.append("/");
} else if (e.getSource() == b[15]) {
sb.append("*");
} else if (e.getSource() == b[20]) {
sb.append("-");
} else if (e.getSource() == b[25]) {
sb.append("+");
}
sb.append(y);
sb.append("=");
sb.append(tf1.getText());
System.out.println(sb);
File f = new File("e://test.txt");
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(new String(sb), 0, sb.length());
bw.flush();

bw.close();
osw.close();
fos.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

}
}
这个为计算的步骤:
0.07.0=7
7.0+0.0=7
7.08.0=8
7.08.0=15.0
但最后的结果+号没有了,请问这是什么问题.我的算法出现了问题还是别的什么,来个高手解答下!
...全文
479 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
筱周 2010-02-09
  • 打赏
  • 举报
回复
现在的问题是我要把计算步骤放入到TXT文件中,但是记录下的计算步骤没有运算符号,请问这是什么原因?
Defonds 版主 2010-02-09
  • 打赏
  • 举报
回复
楼主简要说一下你的思路,还有算法思想。那么长的代码,谁看谁吃得消?
Defonds 版主 2010-02-09
  • 打赏
  • 举报
回复
引用楼主 zc870504 的回复:
计算器的代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class JiSuanQi {
public static void main(String[] args) {
DFrame frame = new DFrame();
frame.setTitle("计算器");
frame.setBackground(Color.lightGray);
frame.setSize(325, 325);
frame.setVisible(true);

}
}
class DFrame extends JFrame implements ActionListener {
JPanel p0, p1, p2, p3;// 定义面板
JTextField tf1;// 定义计算界面和结果显示区
TextField tf2; // 定义缓存区
String[] s = { "Backspace", "CE", "C", "MC", "MR", "MS", "M+", "7", "8",
"9", "/", "平方根", "4", "5", "6", "*", "平方", "1", "2", "3", "-",
"立方", "0", "+/-", ".", "+", "=" };
Button[] b = new Button[s.length];// 定义按钮
String copycontent = "";
StringBuffer str;
StringBuffer sb;
double x, y;// 定义计算数
int z;// 定义事件
static double m;// 定义缓存数

public DFrame() {
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu menu = new JMenu("编辑");
menuBar.add(menu);//编辑菜单

JMenuItem menuItemOne = new JMenuItem("复制");
menuItemOne.addActionListener(this);
menuItemOne.setActionCommand("copy");
JMenuItem menuItemTwo = new JMenuItem("粘贴");
menuItemTwo.addActionListener(this);
menuItemTwo.setActionCommand("paste");//定义复制和粘贴选项

menu.add(menuItemOne);
menu.add(menuItemTwo);

tf1 = new JTextField(27);
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setEnabled(false);
tf1.setText("0");
tf2 = new TextField(10);
tf2.setEditable(false);// 定义计算界面和结果显示区

for (int i = 0; i < s.length; i++) {
b[i] = new Button(s[i]);
b[i].addActionListener(this);
}//定义按钮

p0 = new JPanel();
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();//定义面板

str = new StringBuffer();
p0.add(tf1);
p0.setBounds(10, 25, 300, 40);//将计算界面和结果显示区添加到面板p0中,设置面板位置

p1.setLayout(new GridLayout(1, 4, 10, 0));//定义面板的布局方式
p1.add(tf2);
p1.add(b[0]);
p1.add(b[1]);
p1.add(b[2]);
p1.setBounds(10, 65, 300, 25);//添加按钮到p1,设置面板位置

p2.setLayout(new GridLayout(4, 1, 0, 15));
p2.add(b[3]);
p2.add(b[4]);
p2.add(b[5]);
p2.add(b[6]);
p2.setBounds(10, 110, 40, 150);//添加按钮到p2,设置面板位置

p3.setLayout(new GridLayout(4, 5, 10, 15));
for (int j = 7; j < 27; j++) {
p3.add(b[j]);
}
p3.setBounds(60, 110, 250, 150);//添加按钮到p3,设置面板位置

this.setLayout(null);
this.add(p0);
this.add(p1);
this.add(p2);
this.add(p3);
this.setResizable(false);//将面板添加到窗口中
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == b[1]) {
tf1.setText("0");
str.setLength(0);//清除
} else if (e.getSource() == b[2]) {
tf1.setText("0");
str.setLength(0);//清零
} else if (e.getSource() == b[23]) {
x = Double.parseDouble(tf1.getText().trim());
tf1.setText("" + (-x));//判断正负数
} else if (e.getSource() == b[25]) {
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y = 0d;
z = 0;//计算加法
} else if (e.getSource() == b[20]) {
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y = 0d;
z = 1;//计算减法
} else if (e.getSource() == b[15]) {
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y = 0d;
z = 2;//计算除法
} else if (e.getSource() == b[10]) {
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y = 0d;
z = 3;//计算乘法
} else if (e.getSource() == b[26]) {
str.setLength(0);
switch (z) {
case 0:
tf1.setText("" + (x + y));
break;
case 1:
tf1.setText("" + (x - y));
break;
case 2:
tf1.setText("" + (x * y));
break;
case 3:
if (Double.parseDouble(tf1.getText()) == 0) {
tf1.setText("除数不能为零");
} else {
tf1.setText("" + (x / y));
}
break;
}

} else if (e.getSource() == b[24]) {
if (tf1.getText().trim().indexOf('.') != -1) { //判断小数

} else {
if (tf1.getText().trim().equals("0")) {
str.setLength(0);
tf1.setText((str.append("0" + e.getActionCommand()))
.toString());
} else if (tf1.getText().trim().equals("")) {
} else {
tf1
.setText(str.append(e.getActionCommand())
.toString());
}
}

y = 0d;

} else if (e.getSource() == b[11]) {
x = Double.parseDouble(tf1.getText().trim());
tf1.setText("数字格式异常");
if (x < 0)
tf1.setText("负数没有平方根");
else
tf1.setText("" + Math.sqrt(x));//开根运算
str.setLength(0);
y = 0d;
} else if (e.getSource() == b[16]) {
x = Double.parseDouble(tf1.getText().trim());
tf1.setText("" + (x * x));//平方运算
str.setLength(0);
y = 0d;
} else if (e.getSource() == b[21]) {
x = Double.parseDouble(tf1.getText().trim());
tf1.setText("" + (x * x * x));//立方运算
str.setLength(0);
y = 0d;

} else if (e.getSource() == b[3]) {//缓存数操作
m = 0d;
tf2.setText("");
str.setLength(0);
} else if (e.getSource() == b[4]) {
if (tf2.getText().trim() != "") {
tf1.setText("" + m);
}
} else if (e.getSource() == b[5]) {

m = Double.parseDouble(tf1.getText().trim());
tf2.setText("M");
tf1.setText("0");
str.setLength(0);
} else if (e.getSource() == b[6]) {
m = m + Double.parseDouble(tf1.getText().trim());
} else {
if (e.getSource() == b[22]) {
if (tf1.getText().trim().equals("0")) {

} else {
tf1
.setText(str.append(e.getActionCommand())
.toString());
y = Double.parseDouble(tf1.getText().trim());
}
} else if (e.getSource() == b[0]) {//退后操作
if (!tf1.getText().trim().equals("0")) {
if (str.length() != 1) {
tf1.setText(str.delete(str.length() - 1,
str.length()).toString());
} else {
tf1.setText("0");
str.setLength(0);
}
}
y = Double.parseDouble(tf1.getText().trim());
} else if (!e.getActionCommand().equals("copy")
&& !e.getActionCommand().equals("paste")) {//复制和粘贴操作
tf1.setText(str.append(e.getActionCommand()).toString());
y = Double.parseDouble(tf1.getText().trim());
}
}
} catch (NumberFormatException e1) {
tf1.setText("数字格式异常");
} catch (StringIndexOutOfBoundsException e1) {
tf1.setText("字符串索引越界");
}
if (e.getActionCommand().equals("copy")) {
copycontent = tf1.getText();
}
if (e.getActionCommand().equals("paste")) {
tf1.setText(tf1.getText() + copycontent);
}
try{
StringBuffer sb = new StringBuffer();
sb.append(x);
if (e.getSource() == b[10]) {
sb.append("/");
} else if (e.getSource() == b[15]) {
sb.append("*");
} else if (e.getSource() == b[20]) {
sb.append("-");
} else if (e.getSource() == b[25]) {
sb.append("+");
}
sb.append(y);
sb.append("=");
sb.append(tf1.getText());
System.out.println(sb);
File f = new File("e://test.txt");
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(new String(sb), 0, sb.length());
bw.flush();

bw.close();
osw.close();
fos.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

}
}
这个为计算的步骤:
0.07.0=7
7.0+0.0=7
7.08.0=8
7.08.0=15.0
但最后的结果+号没有了,请问这是什么问题.我的算法出现了问题还是别的什么,来个高手解答下!
代码好多。
stamp80 2010-02-08
  • 打赏
  • 举报
回复
lz挺强,顶一个,头晕不看程序了。

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧