嗨,大家好,我想知道如何創建在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;
}
什麼想法? 謝謝
它的工作,但是當我打印數組它只打印數據不是數據! – Ammar
[問題@ 3d4eac69,問題@ 42a57993,.......等] – Ammar
使用循環打印元素。 –