java编计算器的问题

[复制链接]
查看11 | 回复1 | 2011-3-19 12:49:13 | 显示全部楼层 |阅读模式
代码如下import java.awt.*;
import java.awt.event.*;
class Jstext extends Frame implements ActionListener
{
TextField text1,text2,text3;
Button 加,减,乘,除;
int s,t1,t2;
Jstext()
{
text1=new TextField();
text2=new TextField();
text3=new TextField();
加=new Button(\" \");
减=new Button(\"-\");
乘=new Button(\"*\");
除=new Button(\"/\");
加.addActionListener(this);
减.addActionListener(this);
乘.addActionListener(this);
除.addActionListener(this);
add(text1);
add(text2);
add(text3);
add(加);
add(减);
add(乘);
add(除);
setLayout(new FlowLayout());
setBounds(10,10,400,400);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{

try{
t1=Integer.parseInt(text1.getText());t2=Integer.parseInt(text1.getText());
if(e.getSource()==加)
{s=t1 t2;text3.setText(\"\" s);}
else if(e.getSource()==减)
{s=t1-t2;text3.setText(\"\" s);}
else if(e.getSource()==乘)
{s=t1*t2;text3.setText(\"\" s);}
else if(e.getSource()==除)
{s=t1/t2;text3.setText(\"\" s);}
}
catch(NumberFormatException event)
{

System.out.println(\"请输入数字\");
}
}
}
public class exa21106
{
public static void main(String args[])
{
new Jstext();
}
}



问题是 我运行的时候 结果根本就是错的就练 我填入5 5 它也是等于8是哪里出错了
回复

使用道具 举报

千问 | 2011-3-19 12:49:13 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">说出来你会晕的,你看一下t2获取的是那个框里的值
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行