[英]Storing many Strings in a two-dimensional String array
我正在尝试使用netbeans为学校项目编写Java测验程序。 我大约有40个问题,需要将它们存储在二维数组中。
String qna[][]=new String[40][5];\\\\ for the question and each of the four options which will be displayed on 4 toggle buttons
我的问题是我必须输入大量代码才能加载每个问题及其四个选项,而当我不得不编辑问题时很难。
有没有更有效的方法(例如使用文本文档或其他位置存储的东西)?
您不应该使用2D数组来存储问题和答案,这既不好又脆弱!
您可以创建一个名为CompoundQuestion
的类,其中包含问题和答案,然后创建CompoundQuestion
一些对象。 该类应该是这样的:
public class CompoundQuestion {
private String question;
private String[] answers;
public String getQuestion () { return question; }
public String[] getAnswers () { return answers; }
public CompoundQuestion (String question, String[] answers) {
this.question = question;
this.answers = answers;
}
public CompoundQuestion (String question, String... answers) {
this(question, answers);
}
}
以上只是一个简单的实现。 如果您不了解类的概念,请阅读以下内容:
https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html
如果您不知道String...
在做什么,请阅读以下内容:
https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
如果您仍然不了解,请使用构造函数的第一个重载。
您可以像这样使用您的类(假设您了解String...
部分):
CompoundQuestion myQuestion = new CompoundQuestion ("Is Sweeper handsome?", "Yes!", "YASSS", "Yeeeeeeeesssss");
然后可以将其存储在数组中:
CompoundQuestion[] questionArray = new CompoundQuestion[40];
当然,您可以通过以下方式获取问题的文字和答案:
getQuestion ()
和
getAnswers ()
如果您不理解类,请使用2D数组...我无语。
万一您还想存储正确的答案,可以向CompoundQuestion
类添加一些代码:
private int correctAnswer;
public String getCorrectAnswer () {
return answers[correctAnswer];
}
您也可以将其添加到构造函数中!
编辑:
您也可以将CompoundQuestion
放在文本文件中,以便即使在程序完成后也可以保存它!
使用ObjectInputStream
和ObjectOutputStream
。 后者可以在您计算机上的文件中“序列化” CompoundQuestion
! 并使用前者将其反序列化为CompoundQuestion
对象。
此处有更多信息:
ObjectInputStream
: http : //www.tutorialspoint.com/java/io/java_io_objectinputstream.htm
ObjectOutputStream
: http : ObjectOutputStream
代替使用多维数组,而是创建一个QnA类来存储问题和相应的答案选项。
class QnA {
String question;
String[] answer_options;
}
将您的问题(和相应的答案)存储在json(txt)文件中
[{“ question”:“你好吗”,“ answer_options”:[“不好”,“ OK”,“ GOOD”]},{“ question”:“天空是什么颜色”,“ answer_options”:[“蓝色“,”绿色“,”红色“]}]
使用此文件可填充QnA对象(例如QnA[] Qestions
和数组。
有很多库可以解析json 。
如何创建一个存储问题和所有答案的类问题?
class Question {
private String question;
private List<String> answers;
Question(String question) {
this.question=question;
answers = new ArrayList<>();
}
public void addAnswer(String answer){
this.answers.add(answer);
}
//Getters
}
然后,您可以创建一个类,该类从文本文件读取并使用BufferedReader创建所有问题的列表,如下所示:
public class QuestionLoader {
//Pass the path to your file as the parameter
public List<Question> loadFromFile(String path) {
List<Question> allquestions = new ArrayList<>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File(path)));
Question question = null;
String line = br.readLine();
while (line != null) {
if (line.startsWith("Q::")) {
question = new Question(line.split("::")[1]);
} else if (line.startsWith("A::")) {
question.addAnswer(line.split("::")[1]);
} else if (line.equals("")) {
allquestions.add(question);
}
line = br.readLine();
}
br.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(QuestionLoader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(QuestionLoader.class.getName()).log(Level.SEVERE, null, ex);
}
return allquestions;
}
}
它从结构如下的文件中读取
Q::What is the meaning of life?
A::42
A::33
A::pi
A::all of the above
Q::What?
A::Nothing
A::Something
只需用新行将问题彼此分开。 这样,如果您需要任意数量的问题或答案,则无需更改代码。 它可能不是最有效的方法,但我发现使用列表比使用数组更令人愉快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.