2015-04-07 66 views
0

對於我所在的地區科學博覽會,我正在製作適用於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。有沒有關於如何解決這個問題或者如何爲速度表創建速度限制的提示?

+0

比較像這樣一個浮點數:if(nCurrentSpeed == 1.50f)但你不想使用> =代替==的? – samgak

+0

1.5是雙倍,1.5f是浮動。我仍然不明白你爲什麼要將對象與原始對象進行比較。 –

+0

另外,從來沒有使用==與浮游物或雙打。它非常非常罕見,任何數學上的值都會在浮點世界中精確確定。決定合理的答案可能會被忽略,並檢查答案是否在該範圍內。 –

回答

2

location對象不僅僅是一個原始對象,但其內容在這裏是不知道的。

然而根據後來的代碼,就表明它具有

location.getSpeed() 

因此改變你的代碼,以

else if (location.getSpeed() == 1.50) 

我也建議你使用>= 1.5

+0

謝謝你們的可怕袋熊和gts 101的答案。我是編程新手,所以我對這種事情有點遺忘! – Fraser

1

不應該它只是像

public void onLocationChanged(Location location) { 
    TextView txt = (TextView) this.findViewById(R.id.textView1); 


    if (location==null) 
    { 
     txt.setText("-.- km/h"); 
    } 
    else if (location.getSpeed() >= 1.50f) 
    { 
     txt.setText("Warning"); 
    } 
    else 
    { 
     float nCurrentSpeed = location.getSpeed(); 
     txt.setText(nCurrentSpeed*3.6 + " km/h"); 
    } 

} 

即你需要比較location.getSpeed()與1.5,而不是整個位置對象

+0

是啊,像袋熊說... – gts101

+0

正是我雖然太... –