我正在编写一个与流行应用类似的简单测验应用。谁想成为百万富翁?一切都进行得很好,直到用户回答完第十个问题后我无法完成操作为止。我真正想要的是,在用户回答了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();
}
break;
case R.id.btn_two:
if(btn_two.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
case R.id.btn_three:
if(btn_three.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
case R.id.btn_four:
if(btn_four.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
}
}
private void GameOver(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setMessage("Game Over")
.setCancelable(false)
.setPositiveButton("New Game", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
alertDialogBuilder.show();
}
private void NextQuestion(int num){
tv_question.setText(question.getQuestion(num));
btn_one.setText(question.getchoice1(num));
btn_two.setText(question.getchoice2(num));
btn_three.setText(question.getchoice3(num));
btn_four.setText(question.getchoice4(num));
answer = question.getCorrectAnswer(num);
final String input = tv_question.getText().toString();
t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
Toast.makeText(getApplicationContext(), input, Toast.LENGTH_SHORT).show();
t1.speak(input, TextToSpeech.QUEUE_FLUSH, null);
}
}
});
visible();
}
public void ftft(View v) {
Button Lftft = (Button)findViewById(R.id.button);
Lftft.setEnabled(false);
if(btn_one.getText() == answer){
btn_two.setVisibility(View.INVISIBLE);
btn_three.setVisibility(View.INVISIBLE);
}else{
}
if(btn_two.getText() == answer){
btn_three.setVisibility(View.INVISIBLE);
btn_four.setVisibility(View.INVISIBLE);
}else{
}
if(btn_three.getText() == answer){
btn_one.setVisibility(View.INVISIBLE);
btn_two.setVisibility(View.INVISIBLE);
}else{
}
if(btn_four.getText() == answer){
btn_three.setVisibility(View.INVISIBLE);
btn_one.setVisibility(View.INVISIBLE);
}else{
}
}
private void visible() {
btn_one.setVisibility(View.VISIBLE);
btn_two.setVisibility(View.VISIBLE);
btn_three.setVisibility(View.VISIBLE);
btn_four.setVisibility(View.VISIBLE);
}
public void phone(View v) {
Button phone = (Button)findViewById(R.id.button2);
phone.setEnabled(false);
TextView lifeline = (TextView)findViewById(R.id.textView);
Toast.makeText(getApplicationContext(), "The answer is"+answer, Toast.LENGTH_SHORT).show();
}
public void Audi(View v) {
Button phone = (Button)findViewById(R.id.button3);
phone.setEnabled(false);
TextView lifeline = (TextView)findViewById(R.id.textView);
Toast.makeText(getApplicationContext(), "The answer is"+answer, Toast.LENGTH_SHORT).show();
}
}
下面是我的问题活动代码”
public class Question {
public String questions[] = {
"Which is a Programming Language?",
"In COMAL language program, after name of procedure parameters must be in?",
"Programming language COBOL works best for use in?",
"Are you an M or an m?"
};
public String choices[][] = {
{"HTML", "CSS", "Vala", "PHP"},
{"Punction Marks", "Back-Slash", "Brackets", "Semi Colon"},
{"Siemens Applications", "Student Applications", "Social Applications", "Commercial Applications"},
{"M", "mumu", "SG", "Map"}
};
public String correctAnswer[] = {
"PHP",
"Brackets",
"Commercial Applications",
"M"
};
public String getQuestion(int a){
String question = questions[a];
return question;
}
public String getchoice1(int a){
String choice = choices[a][0];
return choice;
}
public String getchoice2(int a){
String choice = choices[a][1];
return choice;
}
public String getchoice3(int a){
String choice = choices[a][2];
return choice;
}
public String getchoice4(int a){
String choice = choices[a][3];
return choice;
}
public String getCorrectAnswer(int a){
String answer = correctAnswer[a];
return answer;
}
}
答案 0 :(得分:4)
您可以添加一个计数器
Int counter = 0;
用户回答问题后,计数器就会增加。
然后测试计数器> 10
做任何你想做的事
答案 1 :(得分:3)
在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();