小问题

skyboy0720 2003-10-19 05:52:59
import java.awt.*;
import java.applet.*;

public class Testd extends Applet
{
Label lbl_input, lbl_res2, lbl_res3, lbl_res4, lbl_res5;
TextField txt_input, txt_res2, txt_res3, txt_res4, txt_res5;
Button btn_compute;
double res3, res4, res5;
int res2;
public void init ( )
{
lbl_input = new Label ( "请输入一个值:" );
lbl_res2 = new Label ( "精确到整数:" );
lbl_res3 = new Label ( "精确到十分位:" );
lbl_res4 = new Label ( "精确到百分位:" );
lbl_res5 = new Label ( "精确到千分位:" );
txt_input = new TextField ( 5 );
txt_res2 = new TextField ( 5 );
txt_res3 = new TextField ( 5 );
txt_res4 = new TextField ( 5 );
txt_res5 = new TextField ( 5 );
btn_compute = new Button ( "计算" );
add ( lbl_input );
add ( txt_input );
add ( lbl_res2 );
add ( txt_res2 );
add ( lbl_res3 );
add ( txt_res3 );
add ( lbl_res4 );
add ( txt_res4 );
add ( lbl_res5 );
add ( txt_res5 );
add ( btn_compute );
}

public boolean action ( Event e, Object o )
{
Double input = new Double ( o.toString ( ) );
double num = input.doubleValue ( );
compute ( );
return true;
}

public double compute ( )
{
res2 = roundToInteger ( ( int ) num );
res3 = roundToTenths ( num );
res4 = roundToHundredths ( num );
res5 = roundToThousandths ( num );
txt_res2.setText ( Double.toString ( res2 ) );
txt_res3.setText ( Double.toString ( res3 ) );
txt_res4.setText ( Double.toString ( res4 ) );
txt_res5.setText ( Double.toString ( res5 ) );
}

public int roundToInteger ( int number )
{
double y;
y = Math.floor ( number * 10 + .5 );
return ( int ) y;
}

public double roundToTenths ( double number )
{
double y;
y = Math.floor ( number * 10 + .5 ) / 10;
return y;
}

public double roundToHundredths ( double number )
{
double y;
y = Math.floor ( number * 100 + .5 ) / 100;
return y;
}

public double roundToThousandths ( double number )
{
double y;
y = Math.floor ( number * 1000 + .5 ) / 1000;
return y;
}
}


运行错误出现在“NUM”上,请教!!!
...全文
28 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liushenling 2003-10-19
  • 打赏
  • 举报
回复
请注意:
double num = input.doubleValue ( );
你的num是在action函数中声明的,是一个局部变量,在compute中调用肯定会报错,说该变量没有定义!你可以,现在程序顶部声明(就是将它声明为一个全局变量)!
skyboy0720 2003-10-19
  • 打赏
  • 举报
回复
也没什么,就是不能解决对象,反正错误就是在变量NUM上

res2 = roundToInteger ( ( int ) num );
res3 = roundToTenths ( num );
res4 = roundToHundredths ( num );
res5 = roundToThousandths ( num );

显示这四句有错,全是指向NUM!
jan4984 2003-10-19
  • 打赏
  • 举报
回复
把你的错误提示帖出来啦。

62,612

社区成员

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

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