2017-07-15 34 views
-1

我在另一個java類中創建了三個數組:Array Array,Choices Array,Answers Array。現在,我希望在按下選擇(錯誤或錯誤)時,這些問題將隨機隨機洗牌。我的代碼首先是主要活動旁邊的QuestionLibrary。將數組中的隨機問題

package amapps.impossiblequiz; 

public class QuestionLibrary { 





    private String mQuestions[] = { 
      "When was the European Union founded?", 
      "How many Grad Celsius is one Kelvin?", 
      "What is THC?", 
      "How many legs has a spider?", 
      "How many stars has the European flag?", 
      "Which is the seventh planet from the sun?", 
      "What is the chemical formula of salt?", 
      "Who said: Ich bin ein berliner?", 
      "To which country belongs Greenland?", 
      "What is the result of: 2 + 2 *5?", 
      "How many mountains are higher than 8000 meter/26.246 ft?", 
      "A famous quote is: to be, or____ to be!", 
      "What is the name of Stalingrad nowadays?" 

    }; 

    private String mChoices[][] = { 
      {"1993", "1986", "1967"}, 
      {"-260", "-272,15", "279,15"}, 
      {"a plant","The active substance of marijuana" , "a spider"}, 
      {"6", "10","8"}, 
      {"12","15","10"}, 
      {"Uranus","Neptune","Saturn"}, 
      {"HCl","NaCl","CO"}, 
      {"John F. Kennedy", "Richard Nixon","James A. Garfield"}, 
      {"Canada","Denmark", "Greenland is an own state?"}, 
      {"12","20","14"}, 
      {"10","12","14"}, 
      {"not","never","now"}, 
      {"Leningrad","Wolgograd","Dimitrijgrad"} 
    }; 


    private String mCorrectAnswers[] = {"1993", "-272,15", "The active substance of marijuana", "8", "12","Uranus","NaCl","John F. Kennedy","Denmark","12","14","not","Wolgograd"}; 



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

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

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

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

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


    } 



} 

如果正確=隨機問題從陣列/如果假太

 mButtonChoice1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //My logic for Button goes in here 

       if (mButtonChoice1.getText() == mAnswer) { 
        mScore = mScore + 1; 
        updateScore(mScore); 
        updateQuestion(); 

        //This line of code is optional... 
        Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show(); 
        mScore = 0; 
        updateScore(mScore); 
        updateQuestion(); 

       } 
      } 


     }); 

現在,我嘗試了新的代碼洗牌我的數組,但所有的全錯誤的....

包amapps.impossiblequiz;

import java.util.ArrayList; import java.util.List;

公共類問題{

private String question; 
    private String[] choices; 
    private String answer; 

    public Question(String question, String[] choices, String answer) { 
     super(); 
     this.question = question; 
     this.choices = choices; 
     this.answer = answer; 
    } 

    public String getQuestion() { 
     return question; 
    } 

    public String[] getChoices() { 
     return choices; 
    } 

    public String getAnswer() { 
     return answer; 
    } 

} 

//create list 
List<Question> questions = new ArrayList<Question>(); 

//add one question 
questions.add(
     new Question(
       "What's you name?", 
         new String[]{"Foo","Bar","John","Doe"}, 
     "Bar" 
     ) 
     ); 

//add another question 
questions.add(
     new Question(
       "What's you name?", 
         new String[]{"Foo","Bar","John","Doe"}, 
     "Bar" 
     ) 
     ); 

//shuffle questions 
Collections.shuffle(questions); 





public int getlength() { 


    int length = 13; 
    return length; 
} 

}

+1

https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle – frozen

+0

這沒有幫助我什麼,請告訴我在我的代碼裏寫什麼先生! – ProjectX

回答

0

的(真棒)的方式:親自實施費雪耶茨洗牌。它的代碼真的只有幾行:

// mutates original array 
    private static <T> void shuffle(T[] arr){ 
    Random r = new Random(); 
    // iterates back to front 
    for(int i=arr.length-1; i>0; i--){ 
     // swap 
     int k = r.nextInt(i); 
     T tmp = arr[k]; 
     arr[k] = arr[i]; 
     arr[i] = tmp; 
    } 
    } 

最簡單的辦法: Collections.shuffle(Arrays.asList(mQuestions));

+0

你好,我應該把這個插入到我的問題庫中嗎? – ProjectX

0

使用ArrayList來代替,而做到這一點:

Collections.shuffle(yourArrayListName); 
1

這不是一個好主意,讓您的問題和答案在單獨的數組中,因爲你不能重新連接它們,你可以將數組重新連接起來。 我建議寫一個Question類,像這樣:

public class Question { 

    private String question; 
    private String[] choices; 
    private String answer; 

    public Question(String question, String[] choices, String answer) { 
     super(); 
     this.question = question; 
     this.choices = choices; 
     this.answer = answer; 
    } 

    public String getQuestion() { 
     return question; 
    } 

    public String[] getChoices() { 
     return choices; 
    } 

    public String getAnswer() { 
     return answer; 
    } 

} 

,然後用一個列表來管理問題:

//create list 
    List<Question> questions = new ArrayList<Question>(); 

    //add one question 
    questions.add(
      new Question(
        "What's you name?", 
        new String[]{"Foo","Bar","John","Doe"}, 
        "Bar" 
      ) 
    ); 

    //add another question 
    questions.add(
      new Question(
        "What's you name?", 
        new String[]{"Foo","Bar","John","Doe"}, 
        "Bar" 
      ) 
    ); 

    //shuffle questions 
    Collections.shuffle(questions); 

編輯:當然有很多需要提高的關於這個問題類 - 例如,最好有一個.addChoice(String choice)方法,而不必將字符串數組傳遞給構造函數。但這取決於你:)

+0

一些提示 - 你可以在Question構造函數中省略'super()',並使用鑽石運算符'List questions = new ArrayList <>();' – frozen

+0

我肯定會測試這個,我必須改變一些東西在我的主要活動? – ProjectX

+0

無法解析添加,無法解析simbol shuffle?我應該怎麼做? – ProjectX