当我运行我的程序时,当我进行下面的部门时,我的响应总是为0?我已经阅读了一篇关于堆栈的文章,但它似乎更复杂,涉及多个变量,我无法看到它是一样的。如果有人能在我的if和else语句中提供有关数学错误的任何帮助,我将非常感激。对不起,我是新的,这是我的第一语言:/。
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 :(得分:0)
您正在将Spinner
对象与某些int
值进行比较,例如:
if (Conversion.equals(inchesPerFoot))
这是一个毫无意义的比较,永远不会是真的,所以你的计算totalUnits
没有达到任何条件,所以答案是零。
Conversion
的类型为Spinner
,inchesPerFoot
为int
。您需要获取微调器位置索引并与之进行比较,如下所示:
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());
或者,您可以将微调器项内容作为String
获取,并针对不同的转换类型进行String
比较:
String spinnerValue = Conversion.getSelectedItem().toString();
答案 1 :(得分: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));
}
});
}