【发布时间】:2014-04-14 01:21:00
【问题描述】:
我会尽量减少信息,但这个应用程序应该在您每次按下单选按钮时计算一个分数。有 4 个带有单选按钮的屏幕,然后第五个屏幕显示您的分数。
不过,计算总是到数字 4。无论按什么,都没有任何添加。我哪里错了?
RadioButton gpa1 = (RadioButton) findViewById(R.id.GPA1);
RadioButton gpa2 = (RadioButton) findViewById(R.id.GPA2);
RadioButton gpa3 = (RadioButton) findViewById(R.id.GPA3);
RadioButton gpa4 = (RadioButton) findViewById(R.id.GPA4);
//Calculation based on GPA question
if (gpa1.isChecked()){calculation = calculation + 1;}
else{ if (gpa2.isChecked()) {calculation = calculation + 2; }
else{ if (gpa3.isChecked()) {calculation = calculation + 3; }
else{ if (gpa4.isChecked()) {calculation = calculation + 4; }
else{calculation = 0.0; }}}}
这是第一个主要课程,在他们选择答案并按下下一步按钮后,它会进入第二页,其中包含另一个问题和另外 4 个答案,这些答案也应该添加到计算变量中。
这里有更多的代码来显示完全涵盖的内容:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
//RESTORES SAVED STATES
if (savedInstanceState == null ){calculation = 0.0;}
else{calculation = savedInstanceState.getDouble(CALCULATION);}
addListenerOnButton();
RadioButton gpa1 = (RadioButton) findViewById(R.id.GPA1);
RadioButton gpa2 = (RadioButton) findViewById(R.id.GPA2);
RadioButton gpa3 = (RadioButton) findViewById(R.id.GPA3);
RadioButton gpa4 = (RadioButton) findViewById(R.id.GPA4);
//Calculation based on GPA question
if (gpa1.isChecked()){calculation = calculation + 1;}
else{ if (gpa2.isChecked()) {calculation = calculation + 2; }
else{ if (gpa3.isChecked()) {calculation = calculation + 3; }
else{ if (gpa4.isChecked()) {calculation = calculation + 4; }
else{calculation = 0.0; }}}}
//Get button to do button stuff like go to the next page
Button butGPA = (Button) findViewById(R.id.butGPA);
butGPA.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent i = new Intent(Main.this, Main2.class);
startActivity(i);
}
});
}
【问题讨论】:
-
您的
if声明在哪里?是在onCreate()吗? -
是的。我试图减少我在这里输入的信息量,因为我的最后一个问题没有引起太多关注
-
那是你的问题。看我的回答