我正在为学校做一个项目,我在这里遇到了一些麻烦。我要通过一个琐事游戏的文件。文件的格式如下:文件的第一行将是问题的类别,以下几行将是成对的问题和答案。直到它碰到一个空行,这表明空行之后的下一行开始一个新的类别并且它继续。我应该制作一个有 6 个索引的 ArrayList。每个类别一个,然后在每个索引中,我应该有一组问题和答案,我将能够利用这些问题和答案进行比较。我想本质上是一个数组列表中的一个数组。我希望我试图完成的事情是有意义的。这让我很困惑。但这里是我试图工作的代码,有这么多部分我变得非常混乱。
import java.util.ArrayList;
public class TriviaQuestion {
private String player;
private String category;
private String question;
private String answer;
private int score = 0;
/**
*
*/
public TriviaQuestion() {
player = "unknown";
category = "unknown";
question = "unknown";
answer = "unknown";
score = 0;
}
public TriviaQuestion(String category, String question, String answer){
}
public TriviaQuestion(String question, String answer){
}
public TriviaQuestion(String player, String category, String question,
String answer, int score) {
super();
this.player = player;
this.category = category;
this.question = question;
this.answer = answer;
this.score = score;
}
/**
* @return the player
*/
public String getPlayer() {
return player;
}
/**
* @param player the player to set
*/
public void setPlayer(String player) {
this.player = player;
}
/**
* @return the category
*/
public String getCategory() {
return category;
}
/**
* @param category the category to set
*/
public void setCategory(String category) {
this.category = category;
}
/**
* @return the question
*/
public String getQuestion() {
return question;
}
/**
* @param question the question to set
*/
public void setQuestion(String question) {
this.question = question;
}
/**
* @return the answer
*/
public String getAnswer() {
return answer;
}
/**
* @param answer the answer to set
*/
public void setAnswer(String answer) {
this.answer = answer;
}
/**
* @return the score
*/
public int getScore() {
return score;
}
/**
* @param score the score to set
*/
public void setScore(int score) {
this.score = score;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TriviaQuestion [category=" + category + ", question="
+ question + ", answer=" + answer + "]";
}
}
然后是测试人员,我试图在课堂上记下一些关于它的笔记,但在这一点上它们对我来说没有多大意义。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class TriviaQuestion2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File gameFile = new File("trivia.txt");
Scanner inFile = new Scanner(gameFile);
ArrayList<TriviaQuestion> question = new ArrayList<TriviaQuestion> ();
while (inFile.hasNextLine()){
boolean cat = inFile.hasNextLine();
boolean more = true;
while (inFile.hasNextLine() && more);
String answer;
TriviaQuestion temp = new TriviaQuestion(question, answer);
question[0] = new ArrayList<TriviaQuestion>();
new TriviaQuestion(q,a);
question[0].add(temp);
}
}
}
inFile.close();
//use a while loop inside a while loop, and first do category, then question/answer
//there are six categories, use array of trivia questions ArrayList <triviaquestions>[]
//question = new ArrayList<triviaQuesitons>[6]
//question[0] = new ArrayList<triviaQuesitons>();
/**
* while in question[0] and so on read the quesiton and answer
* temp = new tq(question, answer)
* question [0].add(temp)
* question[0].get(i)
*
*/
System.out.println(question);
}
// TODO Auto-generated method stub
}