我正在为高中科学博览会制作速度表应用程序,当超过速度时会显示警告信息。当我的应用程序达到速度限制时,它不会显示该消息。但是当我将else if (location.getSpeed() >= (EditNum*3.6))
部分更改为时
else if (location.getSpeed() >= (EditNum))
该应用有效。问题是,我不想设置MPS的限制。如何使用KM / H转换(* 3.6)运行应用程序? PS我是初学程序员,所以有时我看不到明显的缺陷。
代码:
TextView txt = (TextView) this.findViewById(R.id.textView1);
TextView war = (TextView) this.findViewById(R.id.textView2);
TextView war1 = (TextView) this.findViewById(R.id.textView3);
TextView war2 = (TextView) this.findViewById(R.id.textView4);
TextView war3 = (TextView) this.findViewById(R.id.textView6);
TextView setLimit = (TextView) this.findViewById(R.id.textView5);
try{
EditText Edit = (EditText) findViewById(R.id.editText1);
String EditValue = Edit.getText().toString();
float EditNum = Float.parseFloat(EditValue);
setLimit.setText("Speed limit: " + (EditNum) + " km/h");
if (location==null)
{
txt.setText("-.- km/h");
}
else if (location.getSpeed() >= (EditNum*3.6))
{
war.setTextColor(Color.parseColor("#CC0000"));
war.setText("Warning!!!");
war1.setText("Speed limit: " + (EditNum*3.6) + " km/h" );
war2.setText("Speed: " + (location.getSpeed()*3.6) + " km/h");
war3.setText("Speed limit exceeded by: " + (EditNum*3.6 - location.getSpeed()) + " km/h");
}
else
{
float nCurrentSpeed = location.getSpeed();
txt.setText(nCurrentSpeed*3.6 + " km/h");
}
}
catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
答案 0 :(得分:0)
如何制作转换功能如下:
public float convertSpeedToKmH(float speed) {
return speed * 3.6f;
}
然后调用该函数,而不是直接转换。像:
float locationSpeedInKmH = convertSpeedToKmH(location.getSpeed());
答案 1 :(得分:0)
看起来你的计算错了。你为什么要做limit*3.6f
?
从这一行:setLimit.setText("Speed limit: " + (limit) + " km/h");
,极限变量的值已经是km / h。您应用的转换仅适用于将m / s转换为km / h。要将km / h转换为m / s,则必须除以3.6。