我正在为一个类编写一个简单的Android应用程序,我正在做一个转换程序,它将转换英寸>脚>码码>英里。我有问题弄清楚为什么if和else语句没有结合在一起。我正在粘贴Main.Java,所以你可以看到我有什么,如果我需要发布主xml /字符串请让我知道,但一切都解析出来,直到我得到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 :(得分:0)
写下这样的if条件:
if (unitChoice == feetPerYard)
你需要parens,但你还需要使用==进行比较,single =是一个赋值操作。
答案 1 :(得分:0)
您的问题在这里
if unitChoice = yardsPerMile;
你已经在if条件中添加了分号,删除它会起作用。
if (unitChoice == yardsPerMile)
答案 2 :(得分: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。并将成为String unitChoice
答案 3 :(得分:0)
你的if else语句中有几个错误:
if unitChoice == inchesPerFoot
totalUnits = inchesPerFoot / txtNumberOfUnits;
else
if unitChoice == feetPerYard
totalUnits = feetPerYard / txtNumberOfUnits;
else
if unitChoice == yardsPerMile
totalUnits = yardsPerMile / txtNumberOfUnits;