我正在为高中科学博览会制作一个速度计应用程序,当超过速度时会显示警告消息。当我的应用程序达到速度限制时,它不会显示该消息。但是,当我将 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();
}
最佳答案
在我看来,你的策略没有奏效。快速查看一下,似乎您可能需要除而不是乘以 3.6。
不要将显示的内容与计算的内容混淆。
我建议您在 Debug模式下运行您的应用程序并添加断点(Android Studio 中的红色圆圈),以便您可以查看不同变量在运行时所取的值。然后您将很容易理解为什么满足(或不满足)某些条件。
关于java - 车速表 Android 应用程序无法进行公里/小时转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29570427/