2012-02-08 49 views
1

這是一個基本的單位轉換應用程序。轉換攝氏/華氏時錯誤值

我剛完成編碼,除華氏和攝氏計算外,一切正常。公式是正確的,但不知道爲什麼。

這裏是Activity.Java

package arirang.unit.converter; 

import java.text.DecimalFormat; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.EditText; 
import android.widget.TextView; 


public class BasicUnitConverterActivity extends Activity { 

    EditText val3; 
    String val2; 
    int val1; 
    String forResult; 
    Spinner Type; 


    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Spinner spinner1 = (Spinner) findViewById(R.id.Spinner1); 
     ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
      this, R.array.UnitList, android.R.layout.simple_spinner_item); 
     adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner1.setAdapter(adapter1); 
    } 

    private String changeFormat(Double dbResult) { 
     String result; 

     DecimalFormat format = new DecimalFormat(); 
     result = format.format(dbResult); 
     return result; 
    } 



    public void converter(View View) { 


     EditText val3 = (EditText) findViewById(R.id.BeforeBox); 
     String val2 = val3.getText().toString(); 
     if (val2.length() > 0) { 

      val1 = Integer.parseInt(val2); 
      Type = (Spinner) findViewById(R.id.Spinner1); 

      double dbResult = 0; 

      if (Type.getSelectedItem().toString().equals("meter to inch")) { 
       dbResult = val1 * 39.3700787; 
      } else if (Type.getSelectedItem().toString().equals("meter to feet")) { 
       dbResult = val1 * 3.2808399; 
      } else if (Type.getSelectedItem().toString().equals("feet to inch")) { 
       dbResult = val1 * 12; 
      } else if (Type.getSelectedItem().toString().equals("feet to meter")) { 
       dbResult = val1/3.2808399; 
      } else if (Type.getSelectedItem().toString().equals("inch to meter")) { 
       dbResult = val1/39.3700787; 
      } else if (Type.getSelectedItem().toString().equals("inch to feet")) { 
       dbResult = val1/12; 
      } else if (Type.getSelectedItem().toString().equals("kilogram to pound")) { 
       dbResult = val1 * 2.20462262; 
      } else if (Type.getSelectedItem().toString().equals("pound to kilogram")) { 
       dbResult = val1/2.20462262; 
      } else if (Type.getSelectedItem().toString().equals("Fahrenheit to Celsius")) { 
       dbResult = (val1 - 32) * (5/9); 
      } else if (Type.getSelectedItem().toString().equals("Celsius to Fahrenheit")) { 
       dbResult = (val1 * (9/5)) + 32; 
      } 

      forResult = changeFormat(dbResult); 

      TextView AfterBox = (TextView) findViewById(R.id.AfterBox); 
      AfterBox.setText(forResult); 
     } 
    } 

} 

回答

6

dbResult=(val1 - 32) * (5/9);dbResult=(val1 - 32) * (5f/9);的問題是,當你寫5/9,這意味着你正在分裂int值中,因此結果將是0(5小於9,從而5/9是0.如果是10/9,結果應該是1)。通過將一個強制轉換添加到一個參數中,編譯器執行所謂的擴展原始類型轉換,並將/ operator的兩個參數都浮動。既然分割是在浮點值之間完成的,結果就是你所期望的結果。

1

我肯定會讓「val1」成爲一個浮點數,並在「double」空間中完成所有的數學運算。完成後截斷或舍入爲整數 - 而不是在計算時。

double val1; 
    ... 
    dbResult = (val1 * (9.0/5.0)) + 32.0; 
    ... 

恕我直言...