当前位置: 代码迷 >> Java相关 >> [求助]编写一个运算器
  详细解决方案

[求助]编写一个运算器

热度:365   发布时间:2007-04-28 11:52:21.0
[求助]编写一个运算器
要求是:可以键盘输入数字,还可以用鼠标点击运算器输入数字进行运算!
大家给点意见,思路呀什么的,我是处学者!有代码更好,嘿嘿。。。
搜索更多相关的解决方案: 编写  运算器  

----------------解决方案--------------------------------------------------------
你查一下 论坛里好像有
----------------解决方案--------------------------------------------------------

我刚学JAVA一个礼拜的时候写了个要不要啊 就是可垃圾了:)


----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JSQ1 extends JFrame implements ActionListener
{
private String name[] =
{"0","1", "2","+","3","4","5","-","6","7","8","*","9",".","=","/"};
private String extend[] =
{"x^2","ln","back","sin","cos","tan","√ ̄","off","清空"};
String s="";
int flag=0;
double x;
final double pi = 3.14159265358979323846;
private JButton button[] = new JButton[name.length];
private JButton button1[] = new JButton[extend.length];
JLabel dis_show = new JLabel("结果:");
JTextField window = new JTextField(12);
public JSQ1()
{
super("小小超级计算机");
setSize(200,300);
Container c = getContentPane();
c.setLayout(new GridLayout(2,1,0,0));
JPanel result = new JPanel();
result.add(dis_show);
result.add(window);
for(int i= 0;i<extend.length;i++)
{ button1[i] = new JButton(extend[i]);
result.add(button1[i]);
}
JPanel border = new JPanel();
for(int i= 0;i<name.length;i++)
{
button[i] = new JButton(name[i]);
border.add(button[i]);
}
c.add(result);
c.add(border);

for(int i= 0;i<name.length;i++)
{
button[i].addActionListener(this);
}
for(int i= 0;i<extend.length;i++)
{
button1[i].addActionListener(this);
}
show();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button[3])
{
x=Double.parseDouble(s);
flag=1;
window.setText("");
s="";
}
else if (e.getSource()==button[7])
{
x=Double.parseDouble(s);
flag=2;
window.setText("");
s=""; }
else if (e.getSource()==button[11])
{
x=Double.parseDouble(s);
flag=3;
window.setText("");
s="";
}
else if (e.getSource()==button[15])
{
x=Double.parseDouble(s);
flag=4;
window.setText("");
s=""; }
else if (e.getSource()==button[14])
{
switch(flag)
{
case 1:
{
x=x+Double.parseDouble(s);
String s=String.valueOf(x);
window.setText(s);
break;
}
case 2:
{
x=x-Double.parseDouble(s);
String s=String.valueOf(x);
window.setText(s);
break;
}
case 3:
{
x=x*Double.parseDouble(s);
String s=Double.toString(x);
window.setText(s);
break; }
case 4: {
if(Double.parseDouble(s)==0)
{
window.setText("除数不能为0");
break;
}
x=x/Double.parseDouble(s);
String s=String.valueOf(x);
window.setText(s);
break;
}
}
}
else if(e.getSource()==button1[2])
{
StringBuffer str = new StringBuffer(window.getText());
int n=str.length();
int m=n-1;
s = String.valueOf(str.delete(m,n));
window.setText(s); }
else if(e.getSource()==button1[8])
{
window.setText("");
s="";
flag=0;
}
else if(e.getSource()==button1[0])
{ x=Double.parseDouble(s);
x=x*x;
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[1])
{ x=Math.log(Double.parseDouble(s));
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[3])
{ x=Math.sin(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[6])
{ x=Math.sqrt(Double.parseDouble(s));
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[4])
{ x=Math.cos(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[5])
{
x=Math.tan(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[7])
{ System.exit(0);
} else
{
s=s+e.getActionCommand();
window.setText(s);
} }
public static void main(String[] args)
{
JSQ1 bun = new JSQ1();
bun.addWindowListener(new WindowAdapter()
{
public void windowListener(WindowEvent e)
{System.exit(0); } }); }}
看这个可以不???????????????
----------------解决方案--------------------------------------------------------
回复:(a276202460)我刚学JAVA一个礼拜的时候写了个...
发过来看看,再怎么说我还写不了呢!你比我强!
----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
写的可不好了:)还没注释 既然楼主这么说我就找找传上来
要是想看注释在联系我吧 我的名字就是我的QQ:)
----------------解决方案--------------------------------------------------------
......
刚学一星期就这么强了
有天赋
----------------解决方案--------------------------------------------------------

楼上真是夸奖了 每个人还不是都一样 想学的时候 都能学到东西的 只有向你们学习才会有进步的


----------------解决方案--------------------------------------------------------
回复:(suntianchun)import java.awt.*;import java...
这个程序好象只能用鼠标输入数据,而不能从键盘输入数据?怎么改?大家帮帮忙!
----------------解决方案--------------------------------------------------------
  相关解决方案