如果用户在测验应用程序中回答 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;
    }
}
用户10937486

在主活动中:-

添加班级成员:-

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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在测验应用程序中从 Firestore 中检索好的答案以避免用户在回答问题之前知道好的答案的最佳方法是什么

如何在测验应用程序中创建后退按钮

如果我将相同的发布者ID放入10个应用程序中,该怎么办?

如何在 Windows 10 中杀死 UWP 应用程序?

如何在 Windows 10 中模拟 iOS 应用程序

如何在我的应用程序ios 10中打开默认音乐应用程序

如何降级 Windows 10 应用程序

如何在我的android测验应用程序中为每个问题实现时隙

如果用户在C#程序中打印“ x”,如何使控制台应用程序退出?

iOS Swift3测验应用程序-我应该创建多少个视图来处理5-10种选项

Windows 10:应用程序无法完成UN安装,并保留在应用程序列表中

如何选择只有 w10 兼容应用程序的机器和用户

如何在Windows 10上专门为具有普通帐户的用户安装应用程序?

如何从Windows 10中的所有应用程序列表中删除已卸载的应用程序

为什么我的测验应用程序中的问题之间的导航不起作用?

iOS中的测验应用程序控件,具有不同数量的问题答案

反应问题无法在测验应用程序中读取未定义的属性(读取“地图”)?

测验应用程序中的最后一个分数不会增加/增加

测验应用程序:使用NSUserDefaults保存选定的测验问题并将其显示在另一个视图控制器中

的Windows 10下的Java应用程序的问题放大

.NET MVC / JS / JQUERY中的测验应用程序,如何防止作弊?

如何确保测验应用程序中显示正确的答案选择?

如何在Angular测验应用程序中显示选项1的说明?

如何为聊天应用程序中的民意测验设计数据库架构?

检测 Windows 10 中的第一个应用程序启动

Spring Boot应用程序中的Liquibase保持10个连接打开

带有阵列的测验应用程序,无需重复问题

如何在Electron应用程序中获得Windows 10的主要颜色?

如何在Windows 10移动版应用程序中锁定屏幕?