2012-09-07 26 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;