2015-11-11 193 views
0

我想在一个学校项目中使用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
我的问题是,我必须键入大量的代码来加载每个问题及其'四个选项,当我必须编辑任务时很难。
有没有更有效的方法来做到这一点(如使用文本文件或其他地方存储的东西)?在二维字符串数组中存储很多字符串

+0

你可以将每个'question'和'options'保存在一个文本文件(特殊格式)中并从那里读入。 – 3kings

+2

你为什么不创建一个带有问题字符串和4个选项的类。然后你可以创建这个类的对象数组。 – Atri

+0

@ 3kings我该怎么做? –

回答

2

您不应该使用二维数组存储问题和答案,这是坏的和脆弱的!

您可以创建一个名为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在一个文本文件,这样就可以保存在您的程序完成后也!使用ObjectInputStreamObjectOutputStream。后者将您的计算机上的文件“序列化”CompoundQuestion!然后使用前者将其反序列化为CompoundQuestion对象。

点击此处了解详情:

ObjectInputStreamhttp://www.tutorialspoint.com/java/io/java_io_objectinputstream.htm

ObjectOutputStreamhttp://www.tutorialspoint.com/java/io/java_io_objectoutputstream.htm

+0

OP,顺便说一句,如果你真的想要一个更简单的实现,只需在类中声明一堆变量。基本上,没有getter,没有构造函数,也没有封装。这更简单但是不好的做法。做出明智的选择! – Sweeper

+0

我添加了一些关于如何将文件存储在文件中以实现_late binding_的信息。这样,程序不必使用构造函数初始化所有40个问题。它可以从文件中读取它们。但是,该文件是二进制文件,因此不要尝试编辑它! – Sweeper

0

而不是使用多维数组创建一个类QnA来存储问题和相应的答案选项。

class QnA { 
    String question; 
    String[] answer_options; 
} 

存储您的问题(和相应答案)的JSON(TXT)文件

[ { “问题”: “你怎么样”, “answer_options”: “坏”,“ OK”, “GOOD”] }, { “问题”: “是什么颜色的天空”, “answer_options”: “蓝”, “绿”, “红”] } ]

使用这个文件popul吃了QnA对象的数组,例如QnA[] Qestions

有很多libraries to parse json

0

如何创建一个类的问题,存储问题,它的所有的答案?

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 

只需简单地用新行分隔问题。这样,如果您需要任意数量的问题或答案,您就不需要更改代码。它可能不是最高效的,但我发现使用列表比使用数组更愉快。