首页 > 解决方案 > 如果用户在测验应用程序中回答 10 个问题,如何完成操作

问题描述

我正在编写一个简单的测验应用程序,类似于流行的应用程序谁想成为百万富翁?一切都很顺利,直到用户回答第十个问题后我无法完成操作。我真正想要的是,在用户回答 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;
    }
}

标签: javaandroidandroid-studio

解决方案


您可以添加一个计数器

Int counter = 0;

一旦用户回答了一个问题,计数器就会增加。
然后你测试如果计数器 > 10 做任何你想做的事


推荐阅读