gpt4 book ai didi

java - 我的 EditText 不会为我的分数加分。 Android 测验应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 12:25:43 25 4
gpt4 key购买 nike

为什么当我提交正确答案时,我的程序没有给我的分数加分。请帮忙。这是我的变量:

public class MainActivity extends AppCompatActivity {
int mscore =0 ;
String q5R = "ripken";
String q5CR = "cal ripken";
String q5Cal = "cal";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


public void checkResult(View v) {
//Access the RadioGroup view and save it to a variable.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.q1RadioBox);

//Get the id of the RadioButton that is checked and save it
//as an integer variable.
int solutionId = radioGroup.getCheckedRadioButtonId();

//Use if statements to respond based on whether
//it is the id of the correct answer.
if (solutionId == R.id.answer1) {
mscore = mscore +1;
} else {
mscore = mscore + 0;
}

//Access the RadioGroup2 view and save it to a variable.
RadioGroup radioGroup2 = (RadioGroup) findViewById(R.id.q2RadioBox);

//Get the id of the RadioButton that is checked and save it
//as an integer variable.
int solutionId2 = radioGroup2.getCheckedRadioButtonId();

//Use if statements to respond based on whether
//it is the id of the correct answer.
if (solutionId2 == R.id.answer2) {
mscore = mscore +1;
} else {
mscore = mscore + 0;
}

CheckBox ws1966 = (CheckBox) findViewById(R.id.checkbox_1966);
CheckBox ws1983 = (CheckBox) findViewById(R.id.checkbox_1983);
CheckBox ws1970 = (CheckBox) findViewById(R.id.checkbox_1970);
CheckBox ws1984 = (CheckBox) findViewById(R.id.checkbox_1984);
CheckBox ws1961 = (CheckBox) findViewById(R.id.checkbox_1961);

// Check for Required Answers
if (ws1966.isChecked() && ws1983.isChecked() && ws1970.isChecked() && !ws1984.isChecked() && !ws1961.isChecked()) {
mscore = mscore + 3;
}

EditText questionFive = (EditText) findViewById(R.id.question_5);
String questionFiveAnswer = questionFive.getText().toString();
if (questionFiveAnswer.equalsIgnoreCase(q5R) || questionFiveAnswer.equalsIgnoreCase(q5CR) || questionFiveAnswer.equalsIgnoreCase(q5Cal) ){
mscore = mscore + 1;
}

EditText enterName = (EditText) findViewById(R.id.name_entry);
String name = enterName.getText().toString();

Context context = getApplicationContext();
CharSequence text = name + " your score is: " + mscore + " points out of a possible 6 points";
int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
}


}

最佳答案

在布局文件中创建一个按钮,例如:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="calcScore"
android:id="@+id/button"/>

现在在主 Activity 的 calcScore 方法中编写代码,例如:

public void calcScore(View v)
{
int mscore ;
String q5R = "ripken";
String q5CR = "cal ripken";
String q5Cal = "cal";



EditText questionFive = (EditText) findViewById(R.id.question_5);
String questionFiveAnswer = questionFive.getText().toString();
if (questionFiveAnswer.equalsIgnoreCase(q5R) || questionFiveAnswer.equalsIgnoreCase(q5CR) || questionFiveAnswer.equalsIgnoreCase(q5Cal) ){
mscore = mscore + 1;
}
}

关于java - 我的 EditText 不会为我的分数加分。 Android 测验应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38414774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com