我正在編寫一個簡單的測驗應用程序,類似于流行的應用程序誰想成為百萬富翁?一切都很順利,直到用戶回答第十個問題后我無法完成操作。我真正想要的是,在用戶回答 10 個問題后,我會顯示一條消息,說他們已經達到了第 10 個問題,但我無法做到這一點。public class MainActivity extends Activity implements View.OnClickListener{ TextToSpeech t1; Button btn_one, btn_two, btn_three, btn_four; TextView tv_question; private Question question = new Question(); private String answer; private int questionLength = question.questions.length; Random random; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); random = new Random(); btn_one = (Button)findViewById(R.id.btn_one); btn_one.setOnClickListener(this); btn_two = (Button)findViewById(R.id.btn_two); btn_two.setOnClickListener(this); btn_three = (Button)findViewById(R.id.btn_three); btn_three.setOnClickListener(this); btn_four = (Button)findViewById(R.id.btn_four); btn_four.setOnClickListener(this); tv_question = (TextView)findViewById(R.id.tv_question); NextQuestion(random.nextInt(questionLength)); final String input = tv_question.getText().toString() } public void onPause() { if (t1 != null) { t1.stop(); t1.shutdown(); } super.onPause(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_one: if(btn_one.getText() == answer){ Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show(); NextQuestion(random.nextInt(questionLength)); }else{ GameOver(); }
2 回答

holdtom
TA貢獻1805條經驗 獲得超10個贊
在 MainActivity 中:-
添加班級成員:-
int question_limit = 10;
int question_count = 0;
添加新方法
private void QuestionTen() {
Toast.makeText(getApplicationContext(),"You have reached question 10.",Toast.LENGTH_SHORT);
}
在 NextQuestion 方法中添加一行,例如:-
if (question_count++ == question_limit) QuestionTen();
添加回答
舉報
0/150
提交
取消