2017-01-01 140 views
0

嗨,大家好,我想知道如何創建在Java中的數組中隨機取10串從一組字符串(約96串)的方法...如何獲得10個隨機字符串的字符串數組?

的字符串被形式的文本文件 和我已經創建了一個方法來讀取文件並分配變量線 和我創建問題的陣列也

這是我的代碼讀取該文件,並創建一個問題陣列

q是問題

a ch香料a

b ....

CA是正確答案

public static Question[] readAllQuestions() throws FileNotFoundException { 
    int numberOfQuestions = CountQuestions(); 
    Question [] allQuestions = new Question[numberOfQuestions]; 
    Scanner file = new Scanner (new File("TestBank.txt")); 
    String q = ""; 
    String aa = ""; 
    String bb = ""; 
    String cc = ""; 
    String dd = ""; 
    String ccA = ""; 
    int x = 0 ; 
    int k = 0 ; 
    while (x< (allQuestions.length-1)) { 
     while (file.hasNext() && k == 0) { 
      String ques[] = file.nextLine().split(" "); 
      for (int ww = 0 ; ww< ques.length ; ww++) { 
       q += ques[ww]; 
       q+=" "; 
      } 
      if(ques[0].equals("")) { 
       k++ ; 
      } 
     } 
     while (k == 1) { 
      String a[] = file.nextLine().split(" "); 
      for (int ww = 0; ww< a.length; ww++) { 
       aa += a[ww]; 
       aa+= " "; 
      } 
      if(a[0].equals("")) { 
       k++ ; 
      } 
     } 
     //file.hasNext() && 
     while (k == 2) { 
      String b[] = file.nextLine().split(" "); 
      for (int ww = 0 ; ww< b.length ; ww++) { 
       bb += b[ww]; 
       bb+= " "; 
      } 
      if(b[0].equals("")) { 
       k++; 
      } 
     } 
     while (k == 3) { 
      String c[] = file.nextLine().split(" "); 
      for (int ww = 0; ww<c.length; ww++) { 
       cc += c[ww]; 
       cc+= " "; 
      } 
      if(c[0].equals("")) { 
       k++; 
      } 
     } 
     while (k == 4) { 
      String d[] = file.nextLine().split(" "); 
      for (int ww = 0; ww< d.length; ww++) { 
       dd += d[ww]; 
       dd+= " "; 
      } 
      if(d[0].equals("")) { 
       k++; 
      } 
     } 
     while (k == 5) { 
      String cA[] = file.nextLine().split(" "); 
      for (int ww = 0; ww<cA.length; ww++) { 
       ccA += cA[ww]; 
       ccA += " "; 
      } 
      if(cA[0].equals("")) { 
       k++; 
      } 
     } 
     while (k == 6) { 
      Question question = new Question(q,aa,bb,cc,dd,ccA); 
      allQuestions[x] = question; 
      q=""; 
      aa=""; 
      bb=""; 
      cc=""; 
      dd=""; 
      ccA=""; 
      x++; 
      k=0; 
     } 
    } 
    return allQuestions; 
} 

什麼想法? 謝謝

回答

0

您可以使用隨機類。 array[random.nextInt(array.length)]給出了從0array.length - 1的任何數字索引的數組的元素,因爲在nextInt方法中排除了最後一個索引。

public static String[] getRandomStrings(String [] mainStrings, int i) { 
    String [] randomStrings = new String[i]; 
    Random random = new Random(); 
    for(int index = 0; index < i ; index++){ 
     randomStrings[index] = mainStrings[random.nextInt(mainStrings.length)]; 
    } 
    return randomStrings; 
} 
+0

它的工作,但是當我打印數組它只打印數據不是數據! – Ammar

+0

[問題@ 3d4eac69,問題@ 42a57993,.......等] – Ammar

+0

使用循環打印元素。 –

2

在我看來,你想從字符串數組中選擇10個隨機字符串而不選擇重複。一個簡單的方法是使用Collections::shuffle來隨機排列元素,然後選擇混洗列表的一個子集。這裏有一個例子:

public static void main(String[] args) throws Exception 
{ 
    String[] exStringArray = IntStream.range(0, 96) 
      .mapToObj(Integer::toString) 
      .toArray(size -> new String[size]); 

    System.out.println(getRandomElements(Arrays.asList(exStringArray), 10)); 
} 

private static <T> Collection<T> getRandomElements(Collection<T> c, int maxElements) 
{ 
    List<T> deepCopiedList = new ArrayList<>(c); 
    Collections.shuffle(deepCopiedList); 
    return Collections.unmodifiableList(deepCopiedList.subList(0, Math.max(Math.min(deepCopiedList.size(), maxElements), 0))); 
} 
+0

覆蓋toString – pscuderi

相關問題