2015-03-03 37 views
0

我正在为一个类编写一个简单的Android应用程序,并且正在执行转换程序,该程序将英尺>英尺>码>英里转换。我有问题搞清楚为什么如果和其他声明不会在一起。我正在粘贴Main.Java,所以如果我需要发布主xml /字符串,请告诉我,但是所有内容都解析出来,直到我看到if或else语句时,才能看到我拥有的内容。它说它正在寻找(),但是当我添加它说它正在寻找布尔值时,我已经加倍了,但我解析了上面的文本,所以我认为在解析后双精度应该没问题。我试图让这个尽可能清楚,但这是我编程逻辑之外的第一个编程类,所以我的术语可能不是很好。Android:调试if和else语句

package unitconversion.androidbootcamp.net.unitconversion; 
 

 
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 { 
 

 
    double inchesPerFoot = 12; 
 
    double feetPerYard = 3; 
 
    double yardsPerMile = 1760; 
 
    double txtNumberOfUnits; 
 
    double totalUnits; 
 
    double 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 Converstion = (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) { 
 
       txtNumberOfUnits = Integer.parseInt(units.getText().toString()); 
 
       DecimalFormat number = new DecimalFormat("###,###.##"); 
 
       if unitChoice = inchesPerFoot 
 
         totalUnits = inchesPerFoot/txtNumberOfUnits 
 
       else 
 
       if unitChoice = feetPerYard 
 
         totalUnits = feetPerYard/txtNumberOfUnits 
 
       else 
 
       if unitChoice = yardsPerMile; 
 
         totalUnits = yardsPerMile/txtNumberOfUnits; 
 
       unitChoice = units.getSelectedItem().tostring(); 
 
       result.setText("Total Units for" + unitChoice +" is "+ number.format(totalUnits)); 
 
      } 
 
     }); 
 

 
    } 
 

 

 
    @Override 
 
    public boolean onCreateOptionsMenu(Menu menu) { 
 
     // Inflate the menu; this adds items to the action bar if it is present. 
 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
 
     return true; 
 
    } 
 

 
    @Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 
     // Handle action bar item clicks here. The action bar will 
 
     // automatically handle clicks on the Home/Up button, so long 
 
     // as you specify a parent activity in AndroidManifest.xml. 
 
     int id = item.getItemId(); 
 

 
     //noinspection SimplifiableIfStatement 
 
     if (id == R.id.action_settings) { 
 
      return true; 
 
     } 
 

 
     return super.onOptionsItemSelected(item); 
 
    } 
 
}

回答

0

您的unitChoice是一个字符串。你是否想将选定的单元与文本进行比较?

unitChoice = units.getSelectedItem().tostring(); //<--- You make unitChoice String 
if (unitChoice.equals("inchesPerFoot")) 
    totalUnits = inchesPerFoot/txtNumberOfUnits 
else if (unitChoice.equals("feetPerYard")) 
    totalUnits = feetPerYard/txtNumberOfUnits 
else if (unitChoice.equals("yardsPerMile")) 
    totalUnits = yardsPerMile/txtNumberOfUnits; 

而且您还必须将此double unitChoice;设置为字符串。并将成为String unitChoice

+0

我认为这实际上比预期的UnitChoice更可能是转换。或者我看错了。在微调器中,它给出英寸>英尺,英尺>码和码>英里的3种选择。阅读它有意义吗? – 2015-03-03 01:59:25

+0

是的..你可以改变它为unitChoice.equals(“英寸>英尺”),例如 – 2015-03-03 02:01:28

+0

修复它!谢谢。 – 2015-03-03 02:12:13

0

写的,如果条件是这样的:

if (unitChoice == feetPerYard) 

您需要的括号,但你还需要使用用于比较的==,单=是赋值操作。

0

你的问题就在这里

if unitChoice = yardsPerMile; 

ü添加分号if条件,消除它会工作。

if (unitChoice == yardsPerMile) 
0

在有你的,如果else语句有几个误区:

if unitChoice == inchesPerFoot 
    totalUnits = inchesPerFoot/txtNumberOfUnits; 
else 
if unitChoice == feetPerYard 
    totalUnits = feetPerYard/txtNumberOfUnits; 
else 
if unitChoice == yardsPerMile 
    totalUnits = yardsPerMile/txtNumberOfUnits; 
  • 在if条件使用单一的 '=' 符号(使用双 '==' 代替)
  • 使用分号在错误的地方。