gpt4 book ai didi

java - Android 数组不工作的测验

转载 作者:行者123 更新时间:2023-12-01 13:01:26 25 4
gpt4 key购买 nike

我目前正在使用数组和单选按钮进行 10 个问题的多项选择测验。测验本身使用 android studio 通过 genymotion 在模拟器上运行,但答案和问题以及正确答案字段均显示为空白。我已经使用点击监听器创建了测验,但它似乎没有点击到下一个屏幕。我尝试过更改各种代码,但似乎找不到任何将数组读入单选按钮 a、b、c 和 d 的内容。有人能解决这个问题吗?我已附上代码,我认为代码应该正确递增,尽管我还必须添加 8 个问题。

package com.quiz.newquiz.app;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

private int currentQuestion;
private String [] questions;
private String [] answers;

private Button answerButton;
private Button questionButton;
private TextView questionView;
private TextView answerView;
private EditText answerText;

@Override

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialise();
}

public void initialise()
{
questions = new String[]{"What is the capital of Egypt?","What class are you in?"};
answers = new String[] {"Cairo","Class B"};
answerButton = (Button)findViewById(R.id.AnswerButton);
questionButton = (Button)findViewById(R.id.QuestionButton);
questionView = (TextView)findViewById(R.id.QuestionTextView);
answerView = (TextView)findViewById(R.id.AnswerTextView);
answerText = (EditText)findViewById(R.id.AnswerText);
answerButton.setOnClickListener(new View.OnClickListener(){

@Override

public void onClick(View view){
checkAnswer();
}
});

questionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showQuestion();


}
});
}
public void showQuestion()
{ currentQuestion=0;
if(currentQuestion<10)
currentQuestion++;
questionView.setText(questions[currentQuestion]);
answerView.setText("");
answerText.setText("");
}

public boolean isCorrect(String answer)
{
return (answer.equalsIgnoreCase(answers[currentQuestion]));
}
public void checkAnswer()
{
String answer = answerText.getText().toString();
if(isCorrect(answer))
answerView.setText("You are correct!");
else
answerView.setText("Sorry, the correct answer is " + answers[currentQuestion]);


}
}

最佳答案

在代码中查看这一行:

 public void showQuestion()
{ currentQuestion=0;
if(currentQuestion<10)
currentQuestion++;
questionView.setText(questions[currentQuestion]);
answerView.setText("");
answerText.setText("");
}

您的 currentQuestion 变量始终被分配值 0 并被检查。在您的点击监听器中增加当前问题。

questionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentQuestion++;
showQuestion();

}
});

关于java - Android 数组不工作的测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23485477/

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