2015-03-03 45 views
-3

當我運行我的程序時,我的回答總是0,當我做下面的部門?我已經閱讀了一篇關於堆棧的文章,但它似乎更復雜,涉及多個變量,我看不出它們在哪裏是相同的。如果任何人都可以在我的if和else語句中提供任何關於我的數學問題的幫助,我將非常感激。對不起,我是新來的,這是我的第一語言:/。Android:部門總是0

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.TextView; 

import java.text.DecimalFormat; 


public class MainActivity extends ActionBarActivity { 

    int inchesPerFoot = 12; 
    int feetPerYard = 3; 
    int yardsPerMile = 1760; 
    double unitsRequested; 
    double totalUnits; 
    String unitChoice; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final EditText units = (EditText)findViewById(R.id.txtNumberOfUnits); 
     final Spinner Conversion = (Spinner)findViewById(R.id.Units_Array); 
     Button Submit = (Button)findViewById(R.id.btn_Submit); 
     Submit.setOnClickListener(new View.OnClickListener() { 
     final TextView result = ((TextView)findViewById(R.id.txt_Result)); 
      @Override 
      public void onClick(View v) { 
       unitsRequested = Integer.parseInt(units.getText().toString()); 
       DecimalFormat number = new DecimalFormat("###,###.##"); 
       if (Conversion.equals(inchesPerFoot)) 
         totalUnits = (inchesPerFoot/unitsRequested); 
       else 
       if (Conversion.equals(feetPerYard)) 
         totalUnits = (feetPerYard/unitsRequested); 
       else 
       if (Conversion.equals(yardsPerMile)) 
         totalUnits = (yardsPerMile/unitsRequested); 
       unitChoice = Conversion.getSelectedItem().toString(); 
       result.setText("Total Units for " + unitChoice +" is "+ number.format(totalUnits)); 
      } 
     }); 

    } 

回答

0

你一些int值進行比較一個Spinner對象,如:

if (Conversion.equals(inchesPerFoot)) 

這是一個毫無意義的比較,也永遠不會是真實的,所以沒有你的條件見過計算totalUnits所以你的答案是零。

Conversion的類型是SpinnerinchesPerFootint。你需要要麼得到微調位置索引和比較反對的是,像這樣的:

int spinnerIndex = Conversion.getSelectedItemPosition(); 
if (spinnerIndex == 0) 
    totalUnits = (inchesPerFoot/unitsRequested); 
else 
if (spinnerIndex == 1) 
    totalUnits = (feetPerYard/unitsRequested); 
else 
if (spinnerIndex == 2) 
    totalUnits = (yardsPerMile/unitsRequested); 

(假設你的微調項目的順序),或者獲得所選擇的微調項內容(假設它是包含字符串一個整數),並使用:

int spinnerValue = Integer.parseInt(Conversion.getSelectedItem().toString()); 

或者你可以得到微調項內容作爲StringString比較針對不同的轉換類型:

String spinnerValue = Conversion.getSelectedItem().toString(); 
+0

你曾在這裏寫的是我用得到它的工作的最終解決方案。 – 2015-03-06 13:28:18

0

你就在這裏是我使用它的最終解決方案。

公共類MainActivity擴展ActionBarActivity {

final double inchesPerFoot = 12; 
final double feetPerYard = 3; 
final double yardsPerMile = 1760; 
double unitsRequested; 
double totalUnits; 
String unitChoice; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final EditText units = (EditText)findViewById(R.id.txtNumberOfUnits); 
    final Spinner Conversion = (Spinner)findViewById(R.id.Units_Array); 
    Button Submit = (Button)findViewById(R.id.btn_Submit); 
    Submit.setOnClickListener(new View.OnClickListener() { 
    final TextView result = ((TextView)findViewById(R.id.txt_Result)); 
     @Override 
     public void onClick(View v) { 
      unitsRequested = Integer.parseInt(units.getText().toString()); 
      DecimalFormat number = new DecimalFormat("###,###.##"); 
      unitChoice = Conversion.getSelectedItem().toString(); 

      if (unitChoice.equals("Inches to Feet")) 
       totalUnits = (unitsRequested/inchesPerFoot); 
      else 
      if (unitChoice.equals("Feet to Yards")) 
        totalUnits = (unitsRequested/feetPerYard); 
      else 
      if (unitChoice.equals("Yards to Miles")) 
        totalUnits = (unitsRequested/yardsPerMile); 

      result.setText("Total Units for " + unitChoice +" is "+ number.format(totalUnits)); 
     } 
    }); 

}