一个简单的程序我搞了一天了还没搞定,兄弟们帮我看看好吗???

tocson 2004-09-18 03:10:30
要求编一个整型和浮点型两个数的运算,我在程序中的jia ,jian,cheng,chu代表加减乘除,
我刚刚学了两个星期,可能错误比较可笑,希望大家帮我看看,谢谢!
可能我的一些简单东西很整体思路错误
CODE:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class SimpleArithmetic extends Applet implements ActionListener
{
Label prompt1;
TextField input1;
Label prompt2;
TextField input2;
Label output;
Button jia;
Button jian;
Button cheng;
Button chu;
Button zuidazhi;
Button pingjun;

public void init()
{
prompt1=new Label("请输入一个整数:");
input1=new TextField(4);
prompt2=new Label("请输入一个浮点数:");
input2=new TextField(6);
add(prompt1);
add(input1);
add(prompt2);
add(input2);
add(jia);
add(jian);
add(cheng);
add(chu);
add(zuidazhi);
add(pingjun);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
zuidazhi.addActionListener(this);
pingjun.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
if(e.target==jia)
output.setText("相加值为:"+(Integer.parseInt(input1.getText())+Float.valueOf(input2.getText()))+e.toString());

else if(e.target==jian)
output.setText("相减值为:"+(Integer.parseInt(input1.getText())-Float.valueOf(input2.getText()))+e.toString());
else if(e.target==cheng)
output.setText("相乘值为:"+(Integer.parseInt(input1.getText())*Float.valueOf(input2.getText()))+e.toString());
else if(e.target==chu)
output.setText("相除值为:"+(Integer.parseInt(input1.getText())/Float.valueOf(input2.getText()))+e.toString());
else if(e.target==zuidazhi)
output.setText("最大值为:"+(Integer.parseInt(input1.getText())+Float.valueOf(input2.getText()))+e.toString());
else if(e.target==pingjun)
output.setText("平均值为:"+((Integer.parseInt(input1.getText())+Float.valueOf(input2.getText()))/2)+e.toString());
}
}
...全文
161 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaidu 2004-09-19
  • 打赏
  • 举报
回复
那就应该给分撒,呵呵
tocson 2004-09-18
  • 打赏
  • 举报
回复
搞定了
谢谢各位
特别是kaidu(Roger)
我参考你的
scottwhb 2004-09-18
  • 打赏
  • 举报
回复
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class SimpleArithmetic extends Applet implements ActionListener
{
Label prompt1;
TextField input1;
Label prompt2;
TextField input2;
Label output;
Button jia;
Button jian;
Button cheng;
Button chu;
Button zuidazhi;
Button pingjun;

public void init()
{
prompt1=new Label("请输入一个整数:");
input1=new TextField(4);
prompt2=new Label("请输入一个浮点数:");
input2=new TextField(6);
jia = new Button("加"); //创建jia并初始化,以下类似
jian = new Button("减");
cheng = new Button("乘");
chu = new Button("除");
zuidazhi = new Button("最大值");;
pingjun = new Button("平均值");;
add(prompt1);
add(input1);
add(prompt2);
add(input2);
add(jia);
add(jian);
add(cheng);
add(chu);
add(zuidazhi);
add(pingjun);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
zuidazhi.addActionListener(this);
pingjun.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jia)
output.setText("相加值为:"+String.valueOf(Integer.parseInt(input1.getText()) +Float.parseFloat(input2.getText()))+e.toString());
...
}
}
kaidu 2004-09-18
  • 打赏
  • 举报
回复
除了改e.target为e.getSource()外,你的output,jia ,jian ,cheng ,chu ,zuidazhi , pingjun等对象都没有创建,用以下语句创建:
output = new Label("运算结果 ");
jia = new Button("加");
jian = new Button("减");
cheng = new Button("乘");
chu = new Button("除");
zuidazhi = new Button("最大值");
pingjun = new Button("平均值");
并将其加入到Applet,使用如下语句:
add(output);
add(jia);
add(jian);
add(cheng);
add(chu);
add(zuidazhi);
add(pingjun);
最后还有在转换浮点数的地方的valueOf()方法后加floatValue()方法,并且将e.toString()去掉,刚才的程序中有两句多了个分号:
zuidazhi = new Button("最大值");;
pingjun = new Button("平均值");;
去掉一个就行了。
kaidu 2004-09-18
  • 打赏
  • 举报
回复
楼上说的很对,改正后的代码如下(刚刚调试通过),自己慢慢看吧,修改过的地方已经加了注释了:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class SimpleArithmetic extends Applet implements ActionListener
{
Label prompt1;
TextField input1;
Label prompt2;
TextField input2;
Label output;
Button jia;
Button jian;
Button cheng;
Button chu;
Button zuidazhi;
Button pingjun;

public void init()
{
prompt1 = new Label("请输入一个整数:");
input1 = new TextField(4);
prompt2 = new Label("请输入一个浮点数:");
input2 = new TextField(6);
output = new Label("运算结果 "); //创建output并初始化
jia = new Button("加"); //创建jia并初始化,以下类似
jian = new Button("减");
cheng = new Button("乘");
chu = new Button("除");
zuidazhi = new Button("最大值");;
pingjun = new Button("平均值");;
add(prompt1);
add(input1);
add(prompt2);
add(input2);
add(output); //输出标签添加到Applet,接下来的也是一样
add(jia);
add(jian);
add(cheng);
add(chu);
add(zuidazhi);
add(pingjun);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
zuidazhi.addActionListener(this);
pingjun.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jia) //target改为getSource()
output.setText("相加值为:"+(Integer.parseInt(input1.getText())+Float.valueOf(input2.getText()).floatValue()));

else if(e.getSource()==jian)
output.setText("相减值为:"+(Integer.parseInt(input1.getText())-Float.valueOf(input2.getText()).floatValue())); //浮点数还要用floatValue()方法转换,以下类似。并去掉e.toString(),因为没用
else if(e.getSource()==cheng)
output.setText("相乘值为:"+(Integer.parseInt(input1.getText())*Float.valueOf(input2.getText()).floatValue()));
else if(e.getSource()==chu)
output.setText("相除值为:"+(Integer.parseInt(input1.getText())/Float.valueOf(input2.getText()).floatValue()));
else if(e.getSource()==zuidazhi)
output.setText("最大值为:"+Math.max(Integer.parseInt(input1.getText()),Float.valueOf(input2.getText()).floatValue()));
else if(e.getSource()==pingjun)
output.setText("平均值为:"+((Integer.parseInt(input1.getText())+Float.valueOf(input2.getText()).floatValue())/2));
}
}
scottwhb 2004-09-18
  • 打赏
  • 举报
回复
把所有的e.target==pingjun改一下
e.getSource() == pingjun
tomcatjava 2004-09-18
  • 打赏
  • 举报
回复
真是乱七八糟^_^^_^!

首先比较那个事件源触发的ActionEvent,可以使用e.getSource();不过要强制转换成相应的对象,如是Button事件,就用(Button)e.getSource();
使用e.getActionCommand()返回组件上面的title,然后比较可以确定是那个事件源!

其次字符串转换为相应的基本类型,如int , float!如下:
Integer.parseInt( s );将字符串s转换为int
Float.parseFloat( s );将字符串转换为float
mail25 2004-09-18
  • 打赏
  • 举报
回复
是找错呢还是干嘛啊
shangqiao 2004-09-18
  • 打赏
  • 举报
回复
没有使用过applet,up

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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