我目前正在使用android studio和genymotion在Android上开发一个简单的测验应用程序。该应用程序总共有10个问题,其他8个我将在稍后添加。大多数代码对我来说都很好。但是,有一些事情我无法理解,有些方法表示无论我尝试什么都不使用它们,并且变量使用不正确的答案。基本上,我希望测验提出10个问题,在点击单选按钮后更改屏幕,然后在问题10后告诉用户他们的分数结束。
有没有人知道如何解决这个问题,因为我在我的智慧结束?我是否还有其他需要纠正的错误,我没有注意到?
感谢。
这是用**:
突出显示查询的代码package com.quiz.quizsample.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
//Called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
end = (Button) findViewById(R.id.end);
initialise();
}
private int currentQuestion;
private String [] questions;
private String [] answerA;
private String [] answerB;
private String [] answerC;
private String [] answerD;
private String [] correctAnswer;
Button answerButton;
TextView questionView;
TextView answerView;
TextView resultView;
EditText answerText;
RadioButton A;
RadioButton B;
RadioButton C;
RadioButton D;
Button end;
public void initialise()
{
questions = new String[]{"Which year did the Isle of Wight festival begin?","Why"};
answerA = new String[] {"A","1999"};
answerB = new String[] {"n","k"};
answerC = new String[] {"n","k"};
answerD = new String[] {"n","k"};
correctAnswer = new String[] {"n","k"};
currentQuestion = 0;
answerButton = (Button)findViewById(R.id.AnswerButton);
questionView = (TextView)
findViewById(R.id.QuestionTextView);
A = (RadioButton) findViewById(R.id.radioButtonA);
B = (RadioButton) findViewById(R.id.radioButtonB);
C = (RadioButton) findViewById(R.id.radioButtonC);
D = (RadioButton) findViewById(R.id.radioButtonD);
answerView = (TextView)findViewById(R.id.AnswerTextView);
answerText = (EditText)findViewById(R.id.AnswerText);
}
public void **showQuestion()**
{
currentQuestion=0;
if(currentQuestion<10)
currentQuestion++;
questionView.setText(currentQuestion+1);
A.setText(answerA[currentQuestion+1]);
B.setText(answerB[currentQuestion+1]);
C.setText(answerC[currentQuestion+1]);
D.setText(answerD[currentQuestion+1]);
answerButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
checkAnswer();
}
});
questionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton **answer** = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
showQuestion();
}
});
}//This method increments currentQuestion index, checks if it equal to the size of the array and rest if necessary
public void **getCountQuestion**()
{
int countQuestion=0;
if(countQuestion<10)
countQuestion++;
questionView.setText(questions[countQuestion]);`enter code here`
answerView.setText("");
answerText.setText("");
}
public boolean isCorrect(String answer)
{
return (answer.equalsIgnoreCase(correctAnswer[currentQuestion]));
}
public void checkAnswer()
{
String answer = answerText.getText().toString();
if(isCorrect(answer))
answerView.setText("You're right!");
else
answerView.setText("Sorry, the correct answer is " +correctAnswer[currentQuestion]);
}
public void **getResult**()
{
int numberCorrectAnswers = 0;
int **IncorrectAnswers** = 0;
**IncorrectAnswers** =10 - numberCorrectAnswers;
numberCorrectAnswers = new int[**correctAnswer**];
numberCorrectAnswers++;
{
if (numberCorrectAnswers >6)
resultView.setText("Well done, you passed the quiz.");
}//end if
{
if (numberCorrectAnswers <=7)
resultView.setText("Sorry, you have failed the quiz. Please try again soon.");
}//end if
{
{ if (numberCorrectAnswers >=7)
resultView.setText(" You are a festival expert!");
else if (numberCorrectAnswers <2 )
resultView.setText("You must think that music begins and ends with Oasis. Please try harder next time!");
else if (numberCorrectAnswers <4)
resultView.setText ("You need to get out more to festivals!");
else
resultView.setText("Not bad for a first try");
}
}
}
}//class
答案 0 :(得分:1)
你声明了错误的答案,你指定了错误的答案。但那时你什么都不做。如果它只是持有一个值,但没有其他任何关心,Java编译器将完全按照它应该做的 - 告诉你它没有被使用,并且可能不会为它生成代码。