【发布时间】:2017-01-01 18:08:39
【问题描述】:
大家好,我想知道如何创建一个从 java 数组中的一组字符串(大约 96 个字符串)中随机获取 10 个字符串的方法...
字符串取自文本文件 我已经创建了一种方法来读取文件并将变量分配给行 我还创建了一系列问题
这是我读取文件和创建问题数组的代码
q 是问题
选择一个
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;
}
有什么想法吗? 谢谢
【问题讨论】:
标签: java arrays string java.util.scanner