Java 如何在显示数组中的最后一个元素后切换到另一个活动?
我是编程新手,我的程序有问题..我想在数组中的最后一个元素显示后从主活动切换到结果活动,但我的if-else语句似乎是错误的..我尝试了其他引用,但它不起作用..请帮助Java 如何在显示数组中的最后一个元素后切换到另一个活动?,java,android,arrays,Java,Android,Arrays,我是编程新手,我的程序有问题..我想在数组中的最后一个元素显示后从主活动切换到结果活动,但我的if-else语句似乎是错误的..我尝试了其他引用,但它不起作用..请帮助 private int currentQuestion; private String [] questions; private String [] answers; private Button answerButton; private Button questionButton; private TextView que
private int currentQuestion;
private String [] questions;
private String [] answers;
private Button answerButton;
private Button questionButton;
private TextView questionView;
private TextView answerView;
private EditText answerText;
int score=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init()
{
questions = new String[]{"What is my dog name?", "Who is my Crush?"};
answers = new String[]{"Butchick","Michael"};
currentQuestion = -1;
answerButton=(Button)findViewById(R.id.button1);
questionButton=(Button)findViewById(R.id.button2);
questionView=(TextView)findViewById(R.id.textView1);
answerView=(TextView)findViewById(R.id.textView2);
answerText=(EditText)findViewById(R.id.editText1);
answerButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
checkAnswer();
}});
questionButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(currentQuestion<2){
currentQuestion++;
if(currentQuestion==questions.length)
currentQuestion=0;
questionView.setText(questions[currentQuestion]);
answerView.setText("");
answerText.setText("");
return;
}else{
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}});
}
public boolean isCorrect(String answer){
return(answer.equalsIgnoreCase(answers[currentQuestion]));
}
public void checkAnswer(){
String answer = answerText.getText().toString();
if(isCorrect(answer)){
answerView.setText("Youre Right!!");
score++;
}
else {
answerView.setText("Sorry, the correct answer is "+answers[currentQuestion]);
}
}
}在代码中检查此部分
if(currentQuestion==questions.length)
currentQuestion=0;
删除下面的代码
if(currentQuestion==questions.length)
currentQuestion=0;
就是这样…适当的缩进和更少的空白将有助于提高可读性。专业提示:尝试将代码缩进一点更好。OK fine here更多更改移动currentQuestion++;对上述问题的回答