2017-06-12 149 views
-3

測驗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; 
    } 

} 

回答

0

你可以定義一個列表,添加所有可能的值給它,將它洗。

基本上,創建一個類型爲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()