對於我所在的地區科學博覽會,我正在製作適用於Android的速度計應用程序,並且我想設置速度限制。這是我遇到的麻煩的代碼:我希望把它如何設置速度計Android應用程序的速度限制?
public void onLocationChanged(Location location) {
TextView txt = (TextView) this.findViewById(R.id.textView1);
if (location==null)
{
txt.setText("-.- km/h");
}
else if (location == 1.50)
{
txt.setText("Warning");
}
else
{
float nCurrentSpeed = location.getSpeed();
txt.setText(nCurrentSpeed*3.6 + " km/h");
}
}
這樣,當速度1.50公里每小時它改變了文字「警告」。我一直收到Incompatible operand types Location and double
。我試過這樣做
else if (nCurrentSpeed == 1.50)
{
txt.setText("Warning");
}
但它仍然給我同樣的錯誤,但修改Incompatible operand types float and double
。有沒有關於如何解決這個問題或者如何爲速度表創建速度限制的提示?
比較像這樣一個浮點數:if(nCurrentSpeed == 1.50f)但你不想使用> =代替==的? – samgak
1.5是雙倍,1.5f是浮動。我仍然不明白你爲什麼要將對象與原始對象進行比較。 –
另外,從來沒有使用==與浮游物或雙打。它非常非常罕見,任何數學上的值都會在浮點世界中精確確定。決定合理的答案可能會被忽略,並檢查答案是否在該範圍內。 –