小问题
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”上,请教!!!