如何随机数据不重复

时间:2017-06-12 08:39:52

标签: java android arrays eclipse button

测验1:此代码可以随机化问题,但问题是它们仍然是重复的问题。例如,当用户回答问题时,下一个问题也会查看相同的问题。

任何人都可以帮助我吗?谢谢。

public class Quiz1 extends Activity {

    Button mButtonChoice1, mButtonChoice2, mButtonChoice3;

    TextView mScoreView, mQuestionView;

    private QuestionLibrary mQuestionLibrary = new QuestionLibrary();

    private String mAnswer;
    private int mScore = 0;
    private int mQuestionNumber = mQuestionLibrary.mQuestions.length;

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

        r = new Random();


        mButtonChoice1 = (Button)findViewById(R.id.choice1);
        mButtonChoice2 = (Button)findViewById(R.id.choice2);
        mButtonChoice3 = (Button)findViewById(R.id.choice3);

        mScoreView = (TextView)findViewById(R.id.score);
        mQuestionView = (TextView)findViewById(R.id.question);

        mScoreView.setText(" " + mScore);

        updateQuestion(r.nextInt(mQuestionNumber));


        mButtonChoice1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){

                if (mButtonChoice1.getText() == mAnswer){
                    mScore++;
                    mScoreView.setText(" " + mScore);
                    updateQuestion(r.nextInt(mQuestionNumber));
                    if (mScore == 10){
                        success();
                    }

                    Toast.makeText(Quiz1.this, "betul", Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(Quiz1.this, "salah", Toast.LENGTH_SHORT).show();
                    gameOver();
                }
            }
        });

        mButtonChoice2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){

                if (mButtonChoice2.getText() == mAnswer){
                    mScore++;
                    mScoreView.setText(" " + mScore);
                    updateQuestion(r.nextInt(mQuestionNumber));

                    if (mScore == 10){
                        success();
                    }
                    Toast.makeText(Quiz1.this, "betul", Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(Quiz1.this, "salah", Toast.LENGTH_SHORT).show();
                    gameOver();
                }
            }
        });


        mButtonChoice3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){

                if (mButtonChoice3.getText() == mAnswer){
                    mScore++;
                    mScoreView.setText(" " + mScore);
                    updateQuestion(r.nextInt(mQuestionNumber));
                    if (mScore == 10){
                        success();
                    }
                    Toast.makeText(Quiz1.this, "betul", Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(Quiz1.this, "salah", Toast.LENGTH_SHORT).show();
                    gameOver();
                }
            }
        });

    }
    private void updateQuestion(int num){
        mQuestionView.setText(mQuestionLibrary.getQuestion(num));
        mButtonChoice1.setText(mQuestionLibrary.getChoice1(num));
        mButtonChoice2.setText(mQuestionLibrary.getChoice2(num));
        mButtonChoice3.setText(mQuestionLibrary.getChoice3(num));

        mAnswer = mQuestionLibrary.getCorrectAnswer(num);

    }
    private void gameOver(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Quiz1.this);
        alertDialogBuilder
                .setMessage("Tamat! Skor anda ialah  " + mScore + " markah.")
                .setCancelable(false)
                .setPositiveButton("Cuba lagi",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                startActivity(new Intent(getApplicationContext(), Quiz1.class));
                                finish();
                            }
                        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }


    private void success(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Quiz1.this);
        alertDialogBuilder
                .setMessage("Berjaya! Skor anda adalah " + mScore + " markah.")
                .setCancelable(false)
                .setPositiveButton("Pelajaran seterusnya",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                startActivity(new Intent(getApplicationContext(), LessonMenu.class));
                                finish();
                            }
                        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

QuestionLibrary.java

package com.example.fyp_menjahit;

public class QuestionLibrary {

    public String mQuestions [] = {
            "Apakah nama jahitan di dalam video sebentar tadi?",
            "Jahitan tersebut sesuai digunakan untuk?",
            "Berapakah anggaran jarak untuk menjahit jahitan ini...",
            "Diantara berikut, yang manakah langkah awal sebelum menjahit jahitan jelujur kasar?",
            "Antara berikut,yang manakah merupakan bahan jahitan yang betul untuk menjahit jahitan jelujur kasar?",
            "Apakah alatan jahitan yang boleh digunakan dalam menjahit untuk membetulkan jahitan sekiranya berlaku kesalahan?",
            "Adakah jahitan di dalam video sebentar tadi adalah jahitan kia?",
            "Jahitan tersebut sesuai digunakan untuk?",
            "______________ digunakan untuk melekatkan 2 atau lebih fabrik.",
            "Bahan yang diperlukan untuk menjahit jahitan jelujur kasar?"
    };


    private String mChoices [][] = {
            {"Jelujur Halus", "Jelujur Kasar", "Jelujur Tegak"},
            {"untuk melekatkan 2 atau lebih lapisan fabrik", "untuk menjahit butang", "untuk hiasan"},
            {"10mm", "3-5mm", "6mm"},
            {"memasukkan benang ke dalam jarum", "terus menjahit", "memilih fabrik yang cantik"},
            {"benang,pisau lipat, kain", "benang, jarum, gunting", "kertas, kain, benang"},
            {"gam", "gunting", "surat khabar"},
            {"ya", "tidak", "-"},
            {"untuk melekatkan 2 atau lebih lapisan fabrik", "untuk menjahit butang", "untuk hiasan"},
            {"Jahitan kia", "Jahitan jelujur kasar", "Insang pari"},
            {"benang, besi", "benang, jarum, kain", "besi, kain"}
    };



    private String mCorrectAnswers[] = {"Jelujur Kasar", "untuk melekatkan 2 atau lebih lapisan fabrik", "10mm", "memasukkan benang ke dalam jarum", "benang, jarum, gunting",
            "gunting", "tidak", "untuk melekatkan 2 atau lebih lapisan fabrik", "Jahitan jelujur kasar", "benang, jarum, kain"};




    public String getQuestion(int a) {
        String question = mQuestions[a];
        return question;
    }


    public String getChoice1(int a) {
        String choice = mChoices[a][0];
        return choice;
    }


    public String getChoice2(int a) {
        String choice = mChoices[a][1];
        return choice;
    }

    public String getChoice3(int a) {
        String choice = mChoices[a][2];
        return choice;
    }


    public String getCorrectAnswer(int a) {
        String answer = mCorrectAnswers[a];
        return answer;
    }

}

1 个答案:

答案 0 :(得分:0)

您可以定义一个List,为其添加所有可能的值,然后将其随机播放。

基本上,创建一个Integer类型的新ArrayList,将数字从1添加到mQuestionNumber,然后对列表进行洗牌。然后迭代它。

像这样的东西(基本例子):

public class randomizedListTest {
    private static List<Integer> list = new ArrayList<Integer>();

    public static void main (String[] args) {
        for (int i = 0; i < mQuestionNumber; i++) {
            list.add(i); //adds all values to the list
        }

        Collections.shuffle(list); //this randomizes the list

        for (int i = 0; i < mQuestionNumber; i++) {
            System.out.println(list.get(i)); //This should print the all numbers from 0 to mQuestionNumber in random order.
        }
    }
}

Here's the Javadoc entry on Collections.shuffle()