在二维String数组中存储许多字符串

时间:2015-11-11 04:50:43

标签: java arrays string

我正在尝试使用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
我的问题是我必须输入大量代码来加载每个问题及其问题。四个选项,当我必须编辑questons时很难 有没有更有效的方法(例如使用文本文档或其他地方存储的东西)?

3 个答案:

答案 0 :(得分:2)

你不应该使用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放入文本文件中,以便即使在程序完成后也可以保存它!

使用ObjectInputStreamObjectOutputStream。后者用于“序列化”计算机上文件中的CompoundQuestion!并使用前者将其反序列化为CompoundQuestion对象。

此处提供更多信息:

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

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

答案 1 :(得分:0)

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

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

将您的问题(和相应的答案)存储在json(txt)文件中

[    {       "问题":"你好吗",       " answer_options":[" Bad"," OK"," GOOD" ]    },    {       "问题":"天空是什么颜色",       " answer_options":[" Blue"," Green"," Red" ]    } ]

使用此文件填充QnA对象的数组,例如QnA[] Qestions

有很多libraries to parse json

答案 2 :(得分:0)

创建课程如何存储问题及其所有答案?

IReadOnlyCollection<IWebElement> types = Driver.FindElements(By.CssSelector("div.types"));
foreach (IWebElement type in types)
{
    Assert.Istrue(type.FindElement(By.CssSelector("div.span7")).FindElements(By.TagName("b")).Count > 0);
}

然后你可以创建一个从文本文件中读取的类,并使用BufferedReader为你的所有问题创建一个List,如下所示:

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
}

}

它从结构为

的文件中读取
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;
}

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

相关问题