gpt4 book ai didi

java - 不循环 for 循环 - Java (android)

转载 作者:行者123 更新时间:2023-12-02 07:25:05 25 4
gpt4 key购买 nike

程序运行并启动。当我单击主菜单上的“开始”时,它会显示一个问题( TextView )和 4 个答案(按钮)。文本通过 for 循环分配给 TextView 和 4 个按钮。单击按钮后它永远不会循环。单击按钮时没有任何反应。

我尝试过添加“break”并更改为 while 循环,但无法使其正常工作。我认为这是一个我找不到的简单修复。

任何其他代码结构或建议都是值得赞赏和需要的!

public class QuestionView extends Activity {

Quiz quiz = new Quiz();
ArrayList<Question> queries = quiz.getRandom10();

int correctAnswers = 0;
int wrongAnswers = 0;

int answer = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionviewmain);

TextView question = (TextView)findViewById(R.id.question);

Button answer1 = (Button)findViewById(R.id.answer1);
Button answer2 = (Button)findViewById(R.id.answer2);
Button answer3 = (Button)findViewById(R.id.answer3);
Button answer4 = (Button)findViewById(R.id.answer4);

for(int i = 0; i < 10; i++) {
question.setText(queries.get(i).getQuery());
answer1.setText(queries.get(i).getA1());
answer2.setText(queries.get(i).getA2());
answer3.setText(queries.get(i).getA3());
answer4.setText(queries.get(i).getA4());

answer = queries.get(i).getCorrectAnswer();

answer1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 0) {
correctAnswers++;
} else {
wrongAnswers++;
}
}
});

answer2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 1) {
correctAnswers++;
} else {
wrongAnswers++;
}
}
});

answer3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 2) {
correctAnswers++;
} else {
wrongAnswers++;
}
}
});

answer4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 3) {
correctAnswers++;
} else {
wrongAnswers++;
}
}
});
}
}
}

最佳答案

onCreate 方法仅在创建 Activity 时运行。您想要做的是将以下代码分解为一个单独的方法,并从 onClickLiestener() 中为不同的按钮调用该方法:

question.setText(queries.get(i).getQuery());
answer1.setText(queries.get(i).getA1());
answer2.setText(queries.get(i).getA2());
answer3.setText(queries.get(i).getA3());
answer4.setText(queries.get(i).getA4());

由于此解决方案不包含循环,因此您必须确保每次点击都会增加索引(在本例中为“i”)。

编辑: 建议的解决方案

public class QuestionView extends Activity {

Quiz quiz = new Quiz();
ArrayList<Question> queries = quiz.getRandom10();

int correctAnswers = 0;
int wrongAnswers = 0;

int answer = 0;

int i=0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionviewmain);

Button answer1 = (Button)findViewById(R.id.answer1);
Button answer2 = (Button)findViewById(R.id.answer2);
Button answer3 = (Button)findViewById(R.id.answer3);
Button answer4 = (Button)findViewById(R.id.answer4);

answer1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 0) {
correctAnswers++;
} else {
wrongAnswers++;
}
reloadQuestion();
}
});

answer2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 1) {
correctAnswers++;
} else {
wrongAnswers++;
}
reloadQuestion();
}
});

answer3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 2) {
correctAnswers++;
} else {
wrongAnswers++;
}
reloadQuestion();
}
});

answer4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 3) {
correctAnswers++;
} else {
wrongAnswers++;
}
reloadQuestion();
}
});

reloadQuestion();
}


private void reloadQuestion(){
TextView question = (TextView)findViewById(R.id.question);

Button answer1 = (Button)findViewById(R.id.answer1);
Button answer2 = (Button)findViewById(R.id.answer2);
Button answer3 = (Button)findViewById(R.id.answer3);
Button answer4 = (Button)findViewById(R.id.answer4);

question.setText(queries.get(i).getQuery());

answer1.setText(queries.get(i).getA1());
answer2.setText(queries.get(i).getA2());
answer3.setText(queries.get(i).getA3());
answer4.setText(queries.get(i).getA4());

answer = queries.get(i).getCorrectAnswer();

i++;
}
}

您可能可以通过不同的方式优化代码,但这个建议应该可以解决您的问题。

关于java - 不循环 for 循环 - Java (android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13675872/

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