當我運行我的程序時,我的回答總是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));
}
});
}
你曾在這裏寫的是我用得到它的工作的最終解決方案。 – 2015-03-06 13:28:18