我想檢查strCurrentSpeed
是否大於或等於「10」。 我該怎麼做?我收到無差錯「運營商> =未定義的參數類型的字符串,詮釋」我知道這是很基本的,但我無法找到一個解決辦法如何檢查字符串是否等於或大於某個東西?
public void updateSpeed(CLocation location)
{
float nCurrentSpeed = 0;
if(location!=null)
{
location.setUseMetricUnits(this.useMetricUnits());
nCurrentSpeed = location.getSpeed();
}
Formatter fmt = new Formatter(new StringBuilder());
fmt.format(Locale.US, "%5.1f", nCurrentSpeed);
String strCurrentSpeed = fmt.toString();
strCurrentSpeed = strCurrentSpeed.replace(' ', '0');
String strUnits = "miles/hour";
if (this.useMetricUnits())
{
strUnits = "meters/second";
}
TextView txtCurrentSpeed = (TextView) this.findViewById(R.id.txtCurrentSpeed);
txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits);
if (strCurrentSpeed >= 10){
//do action
}}
而不是檢查的'如果(strCurrentSpeed> = 10)'你爲什麼不只是檢查'如果(nCurrentSpeed> = 10)'?你正在將你的字符串從float中移出嗎? – user2004685
啊,好的。什麼是ncurrentspeed btw?我剛剛下載了這個項目 – abbie
'nCurrentSpeed'是你在函數的第一行聲明的float變量。它通過執行'fmt.format(Locale.US,「%5.1f」,nCurrentSpeed);'來保存實際的值。 – user2004685