2012-09-07 32 views
0

我的程序現在將執行以下操作:通過不同的方法返回雙重計算?

  • 用戶輸入
  • 用戶選擇一個轉換
  • 用戶點擊提交

基於轉換採摘(使用單選按鈕)的值,我需要調用一個方法來執行轉換的計算,然後將轉換返回給case,但由於我返回了double,所以我遇到了問題。

package com.exercise_5; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

private String textValue; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

// Method called when the convert button is clicked 
public void convert(View view) { 
    RadioGroup conRadioGroup = (RadioGroup) findViewById(R.id.conRadioGroup); 
    EditText textValue = (EditText) findViewById(R.id.editText1); 

    switch(conRadioGroup.getCheckedRadioButtonId()) { 
    case R.id.radioCelsiusToFahrenheit: 
// Call the convert method 
     fahrenheitToCelsius(); 
//Return the converted variable 

     break; 

    case R.id.radioFahrenheitToCelsius: 
     fahrenheitToCelsius(); 

     break; 

    default: 
     Log.e("Some class tag", "Invalid id was passed to conversion method doing celsius conversion."); 
     return celsiusToFahrenheit(); 
     break; 
    } 

} 

public void fahrenheitToCelsius(Convert tempCelsius) { 

    double conCelsius = Double.parseDouble(textValue); 

    //Calculate Celsius 
    tempCelsius = ((conCelsius * 9)/5) + 32; 
} 

public double celsiusToFahrenheit(double tempInFahrenheit) { 

    double conFahrenheit = Double.parseDouble(textValue); 

    //Calculate Fahrenheit 
    return ((conFahrenheit * 9)/5) + 32; 
} 

public void clear(View view) { 
    //Reset Appended Strings After Previous Run 
    TextView fahrenheit_TV = (TextView) this.findViewById(R.id.textView1); 
    TextView celsius_TV = (TextView) this.findViewById(R.id.textView3); 

    fahrenheit_TV.setText("Fahrenheit: "); 
    celsius_TV.setText("Celsius: "); 

} 

}

+0

你能準確地告訴你想要什麼嗎? –

+0

基本上我需要的是在啓動case的情況下調用我的華氏方法,然後華氏方法返回一個我可以用來追加字符串的double。 – Snwspeckle

回答

1

對於Kuu,實際上沒有。如果二進制運算中的任何一個變量(加法,乘法,減法,加法,餘數)都是雙精度的,那麼Java會將這兩個值視爲雙精度值,所以在((conCelsius * 9)/ 5)+32

操作是雙重操作。

要回答實際問題,代碼有幾個問題。設置相關單選按鈕的正確方法是將它們分組在單選按鈕組中(我假設您沒有給出代碼)。舉例來說,參見http://www.mkyong.com/android/android-radio-buttons-example/

這樣做後,您可以檢查單選按鈕的ID選擇哪些按鈕。

這對於代碼的總體佈局:

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

    radioTempConvGroup = (RadioGroup) findViewById(R.id.radioTempConvGroup); 
    btnConvert = (Button) findViewById(R.id.btnConvert); 

    btnConvert.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

       // get selected radio button from radioGroup 
      int selectedId = radioTempConvGroup.getCheckedRadioButtonId(); 

        double convertedTemp = convert(selectedId); 
        //do other operations with convertedTemp like display 
     } 

    }); 


    private double convertTemp(int selectedId) { 
     //TODO get value from the textview that holds the value 
     double temperature = //INSERT code to get the value to convert 
     switch(selectedId) { 
     case R.id.radioCelsius: 
      return celsiusToFahrenheit(temperature); 
      break; 
     case R.id.radioFahrenheit: 
      return fahrenheitTocelsius(temperature); 
      break; 
     default: 
      Log.e("Some class tag", "Invalid id was passed to conversion method doing celsius conversion."); 
      return celsiusToFahrenheit(temperature); 
      break; 
     } 
    } 

    private double celsiusToFahrenheit(double tempInCelsius) { 
     //TODO add actual conversion 
    } 

    private double fahrenheitToCelsius(double tempInFahrenheit) { 
     //TODO add actual conversion 
    } 

有幾個問題與您的代碼:

始終使用,如果和類似的結構支架,以防止執行一個指令,而不是幾個。我認爲

case R.id.radioButton1: 
    if (checked) 
     fahrenheit(); 

     //Append Strings 
     fahrenheit_TV.append(" " +fahrenheit.conversion); 

    break; 

的目的是爲

case R.id.radioButton1: 
    if (checked) { 
     fahrenheit(); 

     //Append Strings 
     fahrenheit_TV.append(" " +fahrenheit.conversion); 
    } 
    break; 

但僅如果在您的版本執行後,立即指令。

當原始double值滿足時,不要使用Double。對象創建和裝箱和拆箱是昂貴的操作。

public static double fahrenheit(Double conversion) { 

    Double conCelsius = Double.parseDouble(getCelsius); 

    //Calculate Celsius 
    return ((conCelsius * 9)/5) + 32; 
    } 

應該是:

public static double fahrenheit(double conversion) { 

    double conCelsius = Double.parseDouble(getCelsius); 

    //Calculate Celsius 
    return ((conCelsius * 9)/5) + 32; 
    } 

第三,儘量在整個代碼一致的和/或嘗試使用Java約定,這讓其他人更容易(你6個月後),以閱讀代碼。 一種方法名稱以大寫字母開頭,另一種以小寫字母開頭。一些變量 使用_作爲分隔符,其他使用駱駝大小寫。

+0

非常感謝您清理我的代碼,我從頭開始,非常感謝。 – Snwspeckle

+0

我在返回計算和調用switch語句時遇到問題,請注意展開如何正確執行此操作? – Snwspeckle

+0

問題到底是什麼? –

1

我覺得你這個問題是因爲您的操作與整數(9乘以5分),因此編譯器正在改變conCelsius完成,同時使積分爲int ,而應該是這樣的:

return ((conCelsius * 9.0)/5.0) + 32;