|
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”上,请教!!! |
|
|
|
把你的错误提示帖出来啦。
|
|
|
也没什么,就是不能解决对象,反正错误就是在变量NUM上
res2 = roundToInteger ( ( int ) num ); res3 = roundToTenths ( num ); res4 = roundToHundredths ( num ); res5 = roundToThousandths ( num ); 显示这四句有错,全是指向NUM! |
|
|
请注意:
double num = input.doubleValue ( ); 你的num是在action函数中声明的,是一个局部变量,在compute中调用肯定会报错,说该变量没有定义!你可以,现在程序顶部声明(就是将它声明为一个全局变量)! |
|