所以我得到了以下代码,根据用户输入进行一些计算,然后在textView中显示结果。
public class DescentCalculator extends AppCompatActivity {
EditText num1, num2, num3;
TextView resu;
double startdecent;
double feetminute;
@Override
public void onCreate ( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.descent);
Toolbar mToolbar = (Toolbar) findViewById(R.id.mtoolbar);
setSupportActionBar(mToolbar);
Button add = (Button) findViewById(R.id.button11);
num1 = (EditText) findViewById(R.id.altitude_fix);
num2 = (EditText) findViewById(R.id.altitude_cruise);
num3 = (EditText) findViewById(R.id.mach_speed);
resu = (TextView) findViewById(R.id.answer);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick ( View v ) {
// TODO Auto-generated method stub
String altfix = num1.getText().toString();
String altcruise = num2.getText().toString();
String machspeed = num3.getText().toString();
startdecent = (Double.parseDouble(altcruise) - Double.parseDouble(altfix)) / 100 / 3;
feetminute = (3 * Double.parseDouble(machspeed) * 1000);
resu.setText(Double.toString(startdecent) + Double.toString(feetminute));
}
});
}
例如,如果用户为altcruise输入7000,为altfix输入6000,为machspeed输入0.30,应用程序将答案计算为3.33333333333335899.999999999,这在技术上是正确的。我喜欢这个应用程序来回答答案并在这种情况下显示3.3。
答案 0 :(得分:1)
看看这个答案:Round a double to 2 decimal places
此代码段接受一个double并将其读入BigDecimal并将其舍入返回一个带有n个小数位的double。
public static void main(String[] args){
double myDouble = 3.2314112;
System.out.print(round(n,1));
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
返回3.2