【问题标题】:how to random data without repeat如何随机数据而不重复
【发布时间】:2017-11-13 15:55:30
【问题描述】:

测验 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

包 com.example.fyp_menjahit;

公共类 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;
    }

}

【问题讨论】:

    标签: java android arrays eclipse button


    【解决方案1】:

    您可以定义一个列表,向其中添加所有可能的值,然后随机播放。

    基本上,创建一个新的 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()

    【讨论】:

      猜你喜欢
      • 2011-06-19
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多