2016-06-12 129 views
0

我的問題看起來很簡單,所以希望我能很快找到答案。
爲了總結我的代碼,我生成了一個介於1 & 2之間的隨機數。根據隨機數,我有一個包含easy1easy2"easy" + randomNum)的字符串questionName。我想打印questionName的內容,但不是字符串,請獲取數組easy1[0]easy2[0]str作爲數組名稱的內容

public void generateQuestion() { 
    int min = 1; 
    int max = 2; 


    Random r = new Random(); 
    int questionToBeSelected = r.nextInt(max - min + 1) + min; 

    System.out.println(questionToBeSelected); 

    String questionName = "easy" + questionToBeSelected; 

    String[] easy1 = { 
      "In The Phantom Menace, who are the first two characters on screen?", //Question, obviously 
      "3", //The array child that is correct 
      "Padme and Annie", //Answer 1 
      "Obi-Wan and Qui-Gon Jinn", //Answer 2 
      "Jar Jar Binks", //Answer 3 
      "Annie and Obi-Wan" //Answer 4 
    }; 

    String[] easy2 = { 
      "Sample question", 
      "2", 
      "Sample answer", 
      "Sample answer", 
      "Sample answer", 
      "Sample answer", 
    }; 

    System.out.println(easy1[0]); 

    TextView questionPlaceholder = (TextView) findViewById(R.id.game_question); 
    questionPlaceholder.setText(questionName[0]); // <-------- Problem :(

    String alreadyPlayedQuestions; 
    String questionNumber; 
} 

感謝

回答

1

可否請你看看下面的例子:

public void generateQuestion() { 
    int min = 0; 
    int max = 1; 
    Random r = new Random(); 
    int questionToBeSelected = r.nextInt(max - min + 1) + min; 
    System.out.println(questionToBeSelected); 
    String[] easy1 = { 
      "In The Phantom Menace, who are the first two characters on screen?", //Question, obviously 
      "3", //The array child that is correct 
      "Padme and Annie", //Answer 1 
      "Obi-Wan and Qui-Gon Jinn", //Answer 2 
      "Jar Jar Binks", //Answer 3 
      "Annie and Obi-Wan" //Answer 4 
    }; 
    String[] easy2 = { 
      "Sample question", 
      "2", // array index that is correct 
      "Sample answer", 
      "Wrong answer1", 
      "Wrong answer2", 
      "Wrong answer3", 
    }; 
    String[] questionName = new String[][]{easy1, easy2}[questionToBeSelected]; 
    System.out.println(easy1[0]); 
    TextView questionPlaceholder = (TextView) findViewById(R.id.game_question); 
    System.out.println(questionName[0]); 
    questionPlaceholder.setText(questionName[0]); // <-------- Problem :(
    String alreadyPlayedQuestions; 
    String questionNumber; 
} 

對您的方法做的改變很少。請告訴我這是你想達到或不是。

+0

感謝您的回答!當我回家時我會看看它。 –

0

這將是更容易只儲存在另一個數組兩個數組,並挑選從中隨機元素:

int questionToBeSelected = r.nextInt(1); 

String[] easy1 = { 
     "In The Phantom Menace, who are the first two characters on screen?", //Question, obviously 
     "3", //The array child that is correct 
     "Padme and Annie", //Answer 1 
     "Obi-Wan and Qui-Gon Jinn", //Answer 2 
     "Jar Jar Binks", //Answer 3 
     "Annie and Obi-Wan" //Answer 4 
}; 

String[] easy2 = { 
     "Sample question", 
     "2", 
     "Sample answer", 
     "Sample answer", 
     "Sample answer", 
     "Sample answer", 
}; 

String[][] easy = {easy1, easy2}; 

String[] selectedQuetion = easy[questionToBeSelected]; 
+0

感謝您的回答!無論何時運行,我總是會得到'In the Phantom .....'的印刷,我真的很喜歡它是隨機的,不一樣的。謝謝。 –