日期:2014-05-20 浏览次数:21071 次
import java.io.*;
import java.util.*;
import java.util.List;
class QuestionLibrary {
private static int counter = 1;
public final int id = counter++;// 给每一道question一个唯一的id
private String question;
private List<String> choices;// 每一个问题里都对应有若干个choice,那么把这些choice也存起来吧;
private String answer;
QuestionLibrary(String question, List<String> choices, String answer) {
super();
this.question = question;
this.choices = choices;
this.answer = answer;
}
public String getQuestion() {// 你是用private来修饰question的,那么别的类就只能通过此方法来获取question了
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public List<String> getChoices() {
return choices;
}
public void setChoices(List<String> choices) {
this.choices = choices;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
public class ReadTxtToClass {
List<QuestionLibrary> list = new ArrayList<QuestionLibrary>();// 保存所有question
BufferedReader br = null;
String line = null;
StringBuffer question = null;
List<String> choice = null;
StringBuffer answer = null;
public void fileToLibrary() throws Exception {
list = new ArrayList<QuestionLibrary>();
br = new BufferedReader(new FileReader("test.txt"));
question = new StringBuffer();
choice = new ArrayList<String>();
answer = new StringBuffer();
while ((line = br.readLine()) != null) {//消除文件开头的空行,直到<question>
while(line.trim().equals("<question>")) {//将文件指针移到题目开头,//从<question>下一行开始处理,在下一个<question>或者文件尾结束处理
while (!(line = br.readLine().trim()).equals("<choice>"))
//读取<choice>前的信息
question.append(line + "\n");
while (!(line = br.readLine().trim()).equals("<answer>"))
//读取<answer>前的信息
choice.add(new String(line));
//每一个替有多个选项,每个question的每一个选项之间必须有明显的界限,但文件中没有给出(某些选项是以“.”号结束,但有些没有)
//这里我假定每一行就是一个选项
while ((line = br.readLine()) != null) {
if (!(line = line.trim()).equals("<question>"))// 如果还未到文件尾,读取下一个<question>前的信息
answer.append(line + "\n");
else break;
}
QuestionLibrary q = new QuestionLibrary(new String(question),
new ArrayList(choice), new String(answer));
list.add(q);
question.delete(0, question.length());//把里面的内容清空,以便存放下一道题
choice.removeAll(choice);
answer.delete(0, answer.length());
if(line == null) break;//如果已经到了文件尾
}
if(line == null) break;//如果已经到了文件尾
}
}
public static void main(String args[]) throws Exception {
ReadTxtToClass rttc = new ReadTxtToClass();
rttc.fileToLibrary();//将文件信息转换为试题库
List list = rttc.list;//准备查看表中的信息
for(int i=0; i< list.size(); i++) {
System.out.println("---------问题" + (i+1)+"\n" +((QuestionLibrary)list.get(i)).getQuestion());
System.out.println("---------问题" + (i+1)+"的选项");
for(String temp : ((QuestionLibrary)list.get(i)).getChoices())
System.out.println(temp);
System.out.println("---------问题" + (i+1)+"的答案\n" +((QuestionLibrary)list.get(i)).getAnswer());
}
}
}
/*output:
---------问题1
Analyze the following code:
public class Test {
private int t;
public static void main(String[] args) {
int x;
System.out.println(t);
}
}
---------问题1的选项
The variable t is not initialized and therefore causes errors
The variable t is private and therefore cannot be accessed in the main method
t is non-static and it cannot be referenced in a static context in the main method.
The variable x is not initialized and therefore causes errors.
The program compiles and runs fine.
---------问题1的答案
3
---------问题2
What is Java (in regard to Computer Science) ?
---------问题2的选项
A type of coffee
An object-oriented programming language
An interactive website
none of above
---------问题2的答案
2
---------问题3
Java runs on _______.
---------问题3的选项
Windows
Unix/Linux
Mac
All of the Above
---------问题3的答案
4
---------问题4
What is the main function of any variable ?
---------问题4的选项
To add numbers together
To keep track of data in the memory of the computer
To print words on the screen
To write Java
---------问题4的答案
2
---------问题5
The following statements make length be what number ?
int length;
length = 4;
length ++;
---------问题5的选项
4
5
6
8
---------问题5的答案
5
*/