我正面临一个问题/严重错误,尽管我仍然可以自己解决,但我似乎无法自行解决
在构建应用程序时,我希望应用程序在用户检查以下内容时增加变量storesTotalCorrectAnswer
:
单选按钮(包含正确答案),但我认为它一直有效,直到我开始注意到分数不准确,所以我添加了
祝酒消息和祝酒消息不会显示在应用程序上,所以我知道storesTotalCorrectAnswer
变量
不是按预期工作。
所以大家请问我该如何解决这个错误。
我要实现的目的是当用户检查包含以下内容的单选按钮时使storesTotalCorrectAnswer
退缩
正确的答案。
我尝试了各种方法,包括在代码中几乎所有地方都使用了`if语句,但现在似乎可以正常使用
com.myafrica.africaquiz;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
//this variable stores the total number of correct answers
int storesTotalCorrectAnswer = 0;
//this variable counts the total number of randomQuestions asked
int randomNumberCounter = 0;
//this variable stores the random numbers generated
int randomNumber = 0;
//this variables stores the views for public access to other methods
TextView testQuestionID;
RadioButton answerA_ID;
RadioButton answerB_ID;
RadioButton answerC_ID;
RadioButton answerD_ID;
RadioGroup radioGroup_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the text view and stores it in this variable
testQuestionID = findViewById(R.id.testQuestion);
//get the radio button view and stores it in this variable
answerA_ID = findViewById(R.id.answerA);
answerB_ID = findViewById(R.id.answerB);
answerC_ID = findViewById(R.id.answerC);
answerD_ID = findViewById(R.id.answerD);
//get the radio group ID and stores it for future reference
radioGroup_ID = findViewById(R.id.radioGroup_ID);
}
//When the onclicked() is clicked this method runs
public void GetNextQuestion(View view) {
randomNumberGenerator();
//just to know when the onClicks runs
Toast.makeText(MainActivity.this, "NEXT is clicked", Toast.LENGTH_LONG).show();
}
//the random number generator method
public void randomNumberGenerator() {
//the random number generator
Random ranNum = new Random();
randomNumber = ranNum.nextInt(4) + 1;
//to stop the questions from displaying once the required number is reached
if (randomNumberCounter < 4) {
//to know how many times the random method has been called
while (randomNumber > 0) {
randomNumberCounter++;
switch (randomNumber) {
/* When the randomNumber is generated (number from 1 to 4)
* it matches the case and displays the required question */
case 1:
question1();
break;
case 2:
question2();
break;
case 3:
question3();
break;
case 4:
question4();
break;
}
break;
}
}
// shows a next layout after the question stops showing
else {
Intent secondActivity = new Intent(this, Main2Activity.class);
secondActivity.putExtra("secondActivity", "" + storesTotalCorrectAnswer);
startActivity(secondActivity);
}
}
/*
* This part of the code will contain the questions and answer part of the quiz app.
* Each question and answer is a method that will called when the Next button is clicked in the app.
* When the button is clicked the setText() will automatically (re)set the text in the text layout in the XML
*/
public void question1() {
testQuestionID.setText("Which part of Africa can Ghana be located");
answerA_ID.setText(" A: West Africa");
answerB_ID.setText(" B: East Africa");
answerC_ID.setText(" C: Central Africa");
answerD_ID.setText(" D: North Africa");
//wanted to increment the storesTotalCorrectAnswer when the correct radio button is clicked and displayed a toast message
if (answerA_ID.isChecked()) {
storesTotalCorrectAnswer++;
Toast.makeText(MainActivity.this, "Question 1 Answer is clicked", Toast.LENGTH_LONG).show();
}
radioGroup_ID.clearCheck();
}
public void question2() {
testQuestionID.setText("What is the capital of Nigeria");
answerA_ID.setText(" A: Abuja");
answerB_ID.setText(" B: Kano");
answerC_ID.setText(" C: Lagos");
answerD_ID.setText(" D: Port Harourt");
//wanted to increment the storesTotalCorrectAnswer when the correct radio button is clicked and displayed a toast message
if (answerA_ID.isChecked()) {
storesTotalCorrectAnswer++;
Toast.makeText(MainActivity.this, "Question 2 Answer is clicked", Toast.LENGTH_LONG).show();
}
radioGroup_ID.clearCheck();
}
public void question3() {
Toast.makeText(MainActivity.this, "Question 2 Answer is clicked", Toast.LENGTH_LONG).show();
testQuestionID.setText("Which of these countries are not located in Africa");
answerA_ID.setText(" A: Niger");
answerB_ID.setText(" B: Nigeria");
answerC_ID.setText(" C: Rwanda");
answerD_ID.setText(" D: Non of the Above");
//wanted to increment the storesTotalCorrectAnswer when the correct radio button is clicked and displayed a toast message
if (answerD_ID.isChecked()) {
storesTotalCorrectAnswer++;
Toast.makeText(MainActivity.this, "Question 3 Answer is clicked", Toast.LENGTH_LONG).show();
}
radioGroup_ID.clearCheck();
}
public void question4() {
Toast.makeText(MainActivity.this, "Question 2 Answer is clicked", Toast.LENGTH_LONG).show();
testQuestionID.setText("What is the estimated population of Africa, as at 2015");
answerA_ID.setText(" A: 1.2 Billion");
answerB_ID.setText(" B: 500 Billion");
answerC_ID.setText(" C: 2 Billion");
answerD_ID.setText(" D: 360 million");
//wanted to increment the storesTotalCorrectAnswer when the correct radio button is clicked and displayed a toast message
if (answerA_ID.isChecked()) {
storesTotalCorrectAnswer++;
Toast.makeText(MainActivity.this, "Question 4 is clicked", Toast.LENGTH_LONG).show();
}
radioGroup_ID.clearCheck();
}
/* END OF CODE */
}
答案 0 :(得分:0)
我试过了,它运行良好,唯一的问题是您的if语句。您说过要1到4,但条件是检查> 0和<4表示1到3。每当randomNumber为4时,它将不起作用,因此将if (randomNumberCounter < 4)
更改为if (randomNumberCounter <= 4)
。
答案 1 :(得分:0)
首先尝试使用Logcat或
显示您的价值 if (answerA_ID.isChecked()) {
System.out.println("Right Answer");
}
如果显示答案,则使用此(++ storesTotalCorrectAnswer)代替此(storesTotalCorrectAnswer ++)