Android: 调试 if 和 else 语句
Android: debugging if and else statement
我正在为 class 编写一个简单的 Android 应用程序,并且我正在编写一个转换程序,可以转换英寸 > 英尺 > 码 > 英里。我在弄清楚为什么 if 和 else 语句没有放在一起时遇到了问题。我正在粘贴 Main.Java 这样你可以看到我有什么如果我需要 post 主要的 xml/strings 请告诉我,但是一切都在解析,直到我到达 if 或者 else声明。它说它正在寻找 (),但是当我添加它时它说它正在寻找我有双精度的布尔值,但我正在解析它上面的文本所以我认为双精度在解析后应该没问题。我试图尽可能清楚地说明这一点,但这是我的第一个编程 class 超出编程逻辑的范围,所以我的术语可能不太好。
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);
}
}
像这样写 if 条件:
if (unitChoice == feetPerYard)
你需要parens,但你还需要使用==进行比较,单个=是一个赋值操作。
你的问题在这里
if unitChoice = yardsPerMile;
你已经在 if 条件中添加了分号,删除它就可以了。
if (unitChoice == yardsPerMile)
您的 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
您的 if else 语句中几乎没有错误:
if unitChoice == inchesPerFoot
totalUnits = inchesPerFoot / txtNumberOfUnits;
else
if unitChoice == feetPerYard
totalUnits = feetPerYard / txtNumberOfUnits;
else
if unitChoice == yardsPerMile
totalUnits = yardsPerMile / txtNumberOfUnits;
- 在 if 的条件下使用单个“=”符号(改为使用双“==”)
- 在错误的地方使用了分号。
我正在为 class 编写一个简单的 Android 应用程序,并且我正在编写一个转换程序,可以转换英寸 > 英尺 > 码 > 英里。我在弄清楚为什么 if 和 else 语句没有放在一起时遇到了问题。我正在粘贴 Main.Java 这样你可以看到我有什么如果我需要 post 主要的 xml/strings 请告诉我,但是一切都在解析,直到我到达 if 或者 else声明。它说它正在寻找 (),但是当我添加它时它说它正在寻找我有双精度的布尔值,但我正在解析它上面的文本所以我认为双精度在解析后应该没问题。我试图尽可能清楚地说明这一点,但这是我的第一个编程 class 超出编程逻辑的范围,所以我的术语可能不太好。
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);
}
}
像这样写 if 条件:
if (unitChoice == feetPerYard)
你需要parens,但你还需要使用==进行比较,单个=是一个赋值操作。
你的问题在这里
if unitChoice = yardsPerMile;
你已经在 if 条件中添加了分号,删除它就可以了。
if (unitChoice == yardsPerMile)
您的 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
您的 if else 语句中几乎没有错误:
if unitChoice == inchesPerFoot
totalUnits = inchesPerFoot / txtNumberOfUnits;
else
if unitChoice == feetPerYard
totalUnits = feetPerYard / txtNumberOfUnits;
else
if unitChoice == yardsPerMile
totalUnits = yardsPerMile / txtNumberOfUnits;
- 在 if 的条件下使用单个“=”符号(改为使用双“==”)
- 在错误的地方使用了分号。