Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 随意提问,不要重复_Android - Fatal编程技术网

Android 随意提问,不要重复

Android 随意提问,不要重复,android,Android,我想创建一个测验应用程序,例如我有3个问题。我想随机问他们,等所有问题都回答完后就结束比赛,但现在我没法这么做。它正在显示随机问题,但当前会重复。任何帮助都将不胜感激。提前谢谢 这是我的密码: package com.app.twelveimams; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.AlertDialog; import an

我想创建一个测验应用程序,例如我有3个问题。我想随机问他们,等所有问题都回答完后就结束比赛,但现在我没法这么做。它正在显示随机问题,但当前会重复。任何帮助都将不胜感激。提前谢谢

这是我的密码:

package com.app.twelveimams;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class Quiz extends AppCompatActivity implements View.OnClickListener {

    Button btn_one, btn_two, btn_three, btn_four;
    TextView tv_question;

    private Questions question = new Questions();

    private String answer;
    private int questionLength = question.questions.length;

    Random random;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quiz);

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

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_one:
                if(btn_one.getText() == answer){
                    Toast.makeText(Quiz.this, "Correct", Toast.LENGTH_SHORT).show();
                    NextQuestion(random.nextInt(questionLength));
                }else{
                    Toast.makeText(Quiz.this, "Incorrect", Toast.LENGTH_SHORT).show();
                    NextQuestion(random.nextInt(questionLength));
                }

                break;

            case R.id.btn_two:
                if(btn_two.getText() == answer){
                    Toast.makeText(Quiz.this, "Correct", Toast.LENGTH_SHORT).show();
                    NextQuestion(random.nextInt(questionLength));
                }else{
                    Toast.makeText(Quiz.this, "Incorrect", Toast.LENGTH_SHORT).show();
                    NextQuestion(random.nextInt(questionLength));
                }

                break;

            case R.id.btn_three:
                if(btn_three.getText() == answer){
                    Toast.makeText(Quiz.this, "Correct", Toast.LENGTH_SHORT).show();
                    NextQuestion(random.nextInt(questionLength));
                }else{
                    Toast.makeText(Quiz.this, "Incorrect", Toast.LENGTH_SHORT).show();
                    NextQuestion(random.nextInt(questionLength));
                }

                break;

            case R.id.btn_four:
                if(btn_four.getText() == answer){
                    Toast.makeText(Quiz.this, "Correct", Toast.LENGTH_SHORT).show();
                    NextQuestion(random.nextInt(questionLength));
                }else{
                    Toast.makeText(Quiz.this, "Incorrect", Toast.LENGTH_SHORT).show();
                    NextQuestion(random.nextInt(questionLength));
                }

                break;
        }
    }

    private void GameOver(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Quiz.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);
    }
}

您可以使用
Collections.shuffle(列表)
对问题列表进行随机排序

package com.kundan.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Question {

    public static void main(String[] args) {

        ArrayList<String> questionList = new ArrayList<String>();
        questionList.addAll(Arrays.asList("Question 1", "Question 2", "Question 3", "Question 4", "Question 5"));
        System.out.println(questionList);
        Collections.shuffle(questionList);
        System.out.println(questionList);
    }
}
package com.kundan.example;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
公开课问题{
公共静态void main(字符串[]args){
ArrayList questionList=新建ArrayList();
问题列表.addAll(数组.asList(“问题1”、“问题2”、“问题3”、“问题4”、“问题5”);
系统输出打印LN(问题列表);
收集。洗牌(问题列表);
系统输出打印LN(问题列表);
}
}
请参阅下面的链接


注意:字符串不会使用==进行比较,而不是使用随机生成器,存储一个乱序数字列表,然后在为该数字提问时将其删除。您可以给出一个如何进行比较的示例代码吗?将
问题设为Arraylist而不是array,您已经完成了一半。当你得到下一个问题时,把它从你的列表中删除,直到没有问题为止。请随意回答您的问题,以显示您在这方面的尝试