2016-06-12 42 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 .....'的印刷,我真的很喜欢它是随机的,不一样的。谢谢。 –