2017-06-12 11 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()

関連する問題