小问题

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”上,请教!!!
...全文
33 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
  • 打赏
  • 举报
回复
把你的错误提示帖出来啦。
微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 微信小程序系统教程共有“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[中级阶段]——核心技术”、“微信小程序系统教程[阶段]客服消息+微信支付+九宝电商系统”。 “微信小程序系统教程[阶段]全套课程”包含: 1.微信小程序系统教程[阶段]_客服消息 2.微信小程序系统教程[阶段]_微信支付 3.微信小程序系统教程[阶段]_九宝电商系统 学习“微信小程序系统教程[阶段]”要求有微信小程序的基础。建议先学习“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[中级阶段]”,后在报名“微信小程序系统教程[阶段]”。 阶段讲解的客服消息,是针对小程序的。后台程序用接近底层的技术,没有使用三方技术。这样降低同学们学习成本。 微信支付,这部分课程很有难度,要求同学们认真听讲,有不会的技术问题可以请教老师。购买课程后请联系老师,提供单号,给你源程序。 九宝电商系统是一套十分适和学习、项目的课程。既可以系统的学习微信小程序相关知识,还可以修改后上线。 “微信小程序系统教程[中级阶段]——核心技术”重点讲解微信小程序事件、组件、API 微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 购买课程的同学,可赠送就九宝老师编写的《微信小程序开发宝典》。 购课请咨询qq2326321088

62,614

社区成员

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

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