我想在一个学校项目中使用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
我的问题是,我必须键入大量的代码来加载每个问题及其'四个选项,当我必须编辑任务时很难。
有没有更有效的方法来做到这一点(如使用文本文件或其他地方存储的东西)?在二维字符串数组中存储很多字符串
回答
您不应该使用二维数组存储问题和答案,这是坏的和脆弱的!
您可以创建一个名为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()
and
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://www.tutorialspoint.com/java/io/java_io_objectoutputstream.htm
而不是使用多维数组创建一个类QnA来存储问题和相应的答案选项。
class QnA {
String question;
String[] answer_options;
}
存储您的问题(和相应答案)的JSON(TXT)文件
[ { “问题”: “你怎么样”, “answer_options”: “坏”,“ OK”, “GOOD”] }, { “问题”: “是什么颜色的天空”, “answer_options”: “蓝”, “绿”, “红”] } ]
使用这个文件popul吃了QnA对象的数组,例如QnA[] Qestions
。
如何创建一个类的问题,存储问题,它的所有的答案?
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
只需简单地用新行分隔问题。这样,如果您需要任意数量的问题或答案,您就不需要更改代码。它可能不是最高效的,但我发现使用列表比使用数组更愉快。
- 1. 在二维数组中存储字符串和字符(C)
- 2. C在动态二维数组中存储字符串文字
- 3. 二维字符数组字符串
- 4. 存储字符串...二维数组中的数组
- 5. 查找字符串的二维数组中的字符串数
- 6. 二维指针存储字符串
- 7. 如何在C中的多维数组中存储字符串?
- 8. 将大量的字符串存储在多维数组中
- 9. 在多维数组中存储和检索字符串
- 10. 将二维字符串数组存储到SQLite数据库
- 11. 保存输入字符串字符到一个二维数组
- 12. 如何在二维字符数组中存储字符
- 13. 查找C字符串中的二维字符数组中的字符串
- 14. 在字符串中存储字符串
- 15. 打印字符串的二维数组作为字符串
- 16. 我如何插入字符串的二维数组字符串
- 17. angular2很多字符串值存储
- 18. 将字符串的地址存储在字符串数组中
- 19. 将部分字符串存储在字符串数组中
- 20. 在数组中存储字符串并拆分字符串
- 21. 字符串图案在多维字符串数组搜索[,]
- 22. 如何在字符串数组中存储更多数量的字符串
- 23. 比较字符串的二维数组
- 24. PHP转换二维数组字符串
- 25. 比较字符串二维数组
- 26. C#二维数组和字符串
- 27. 在SharedPreferences中存储字符串数组
- 28. 复制从文件中读取的字符串并将其存储在二维字符串数组中
- 29. PHP中多维数组的字符串
- 30. C多维字符串数组在C
你可以将每个'question'和'options'保存在一个文本文件(特殊格式)中并从那里读入。 – 3kings
你为什么不创建一个带有问题字符串和4个选项的类。然后你可以创建这个类的对象数组。 – Atri
@ 3kings我该怎么做? –