网友您好, 请在下方输入框内输入要搜索的题目:
下面是一个Applet程序,其功能是通过两个文本框获得用户输入的两个数,按回车键计算这两个数的商。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
源程序文件代码清单如下:
import java.awt.*;
import java.awt.Event.*;
import java.applet.*;
/*
<applet code="ex5_3.class"width=800 height=400>
</applet>
*/
public class ex5_3 extends Applet
{
private TextField tf1,tf2;
private String strAnswer;
public void init()
{
tf1=new TextField(20);
add(tf1);
tf2=new TextField(20);
add(tf2);
strAnswer="Undefined";
}
public void paint(Graphics g)
{
Font f=new Font("TimesRoman",Font.PLAIN,20);
g.setFont(f);
g.drawstring("result:",80,100);
g.drawstring(strAnswer,60,150);
}
public boolean action(Event e,Object o)
{
String s1=tf1.getText();
String s2=tf2.getText();
try
{
int n1=String.valueOf(s1);
int n2=String.valueOf(s2);
int ut=n1/n2;
StrAnswer=Integer.parseInt(out);
}
catch(NumberFormatException nfe)
{
strAnswer="error number!";
}
repaint();
return true;
}
}
ex5_3.html
<HTML>
<HEAD>
<TITLE>ex5_3</TITLE>
</HEAD>
<BODY>
<applet code="ex5_3.class" width=800 height=400>
</applet>
</BODY>
</HTML>
参考答案