有没有办法在Java的一个数组元素中包含多行文本?

时间:2017-01-02 21:37:50

标签: java arrays eclipse

为学校项目创建一个测验,如果有一种方法可以在一个数组的一个元素中有一个问题和4个可能的答案,那么这将使代码变得更容易,问题和4个可能的答案在他们自己的路线。

4 个答案:

答案 0 :(得分:1)

如果您创建String[]String个对象数组),那么该数组中的每个元素都可以包含任何有效的Java String,其中包含一个包含换行符{String}的字符串}字符。所以你可以让每个数组元素都是一个多行字符串,每行由\n字符分隔,以便在第一个\n之前找到问题,在第二个\n之前找到正确答案{ {1}},并在后续的“行”中找到错误的答案。

然而,我会说这实际上使代码更加神秘,因此很难遵循。更好的,面向对象的方法是创建一个新类,它代表一个测验问题及其答案。这个类的大纲可能如下所示:

\n

你可以完全像这样离开这个类,并简单地引用它的类字段(来自同一个代码包中),从而创建一个新的对象类型,它可以很好地捆绑问题文本和答案。或者,如果需要,您可以向此类添加方法,以控制/保护访问/修改类字段数据的方式。

现在,您可以使用class QuizQuestion { String questionText; String correctAnswer; List<String> incorrectAnswers; } String[],而不是使用原始List<QuizQuestion>。这应该使代码更易于阅读,并且将来更容易增强。

答案 1 :(得分:1)

虽然上面的所有答案都是你问题的有效实现,但我宁愿采用面向对象的方法,正如JB Nizet在上面的评论中所说的那样。这是一个实现测验并显示面向对象实现的示例程序。请不要以1:1的比例复制它作为您作业的解决方案。它应该提供一些使用Java或任何其他面向对象语言可以做的事情的例子......

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Quiz is a "wrapper class" for your whole project. 
 * It contains a set of questions and some helper methods
 * to create the questions and proper answers.
 */
public class Quiz {

    /**
     * We use a list to store our questions. 
     * Each question is an object of type "Question"
     */
    List<Question> questions = new ArrayList<>();

    /**
     * The entry point for our program. 
     * Java will run this method, if our code is "started".
     *
     * All it does is
     *
     * 1. Create a new quiz
     * 2. Start the quiz with the 'play' method
     */
    public static void main(String[] args) {
        Quiz quiz = Quiz.create();
        quiz.play();
    }

    /**
     * Helper method to create a quiz. 
     * You can add as many questions as you like with as many answers 
     * as you like.
     * The second parameter indicates the index of the answer 
     * that is the expected answer (starting with 1, not with 0).
     */
    public static Quiz create() {
        Quiz quiz = new Quiz();
        quiz.addQuestion(
                "What is the heaviest animal on earth?",
                3,
                "Elephant",
                "Cow",
                "Whale",
                "Crocodile"
        );
        quiz.addQuestion(
                "What is the biggest planet in the solar system?",
                2,
                "Mars",
                "Jupiter",
                "Earth",
                "Saturn"
        );
        return quiz;
    }

    /**
     * Helper method that actually starts our quiz.
     *
     * There is a lot of room for improvement here - I just wanted 
     * to give you a brief example
     * of how you can use the code here to play the quiz. Feel free to change :)
     */
    public void play() {
        for (Question q : questions) {
            System.out.println(q.getQuestion());
            int i = 1;
            for (Answer a : q.getAnswers()) {
                System.out.println(i++ + ". " + a.getAnswer());
            }
            System.out.printf("Please tell me the correct answer: ");
            Scanner in = new Scanner(System.in);
            String givenAnswer = in.next();
            if (q.getExpectedAnswer() == Integer.parseInt(givenAnswer)) {
                System.out.printf("Brilliant - your answer was correct!");
            } else {
                System.out.println("Ooops - that was wrong. The expected answer was: " + q.getProperAnswer());
            }
        }
    }

    /**
     * Helper method that adds a question to the quiz.
     *
     * First parameter is the question itself.
     * Second parameter is the index of the correct answer 
     *   in the answers given in the third parameter.
     *   Mind that the index starts with 1 not with 0 as arrays do in Java.
     * Third parameter is a variable length parameter: 
     *   this enables you to add as many answers as you want.
     */
    private void addQuestion(String questionText, int expectedAnswer, String... answers) {
        Question question = new Question(questionText);
        for (String answerText : answers) {
            question.addAnswer(new Answer(answerText));
        }
        question.setExpectedAnswer(expectedAnswer);
        this.questions.add(question);
    }

    /**
     * Class that implements a question.
     *
     * A question consists of the text for the question itself,
     * the index of the expected answer (starting with 1!) and
     * an ordered list of answers.
     */
    public class Question {

        private String question;

        private int expectedAnswer;

        private List<Answer> answers = new ArrayList<>();

        public Question(String question) {
            this.question = question;
        }

        public void addAnswer(Answer answer) {
            answers.add(answer);
        }

        public void setExpectedAnswer(int expectedAnswer) {
            this.expectedAnswer = expectedAnswer;
        }

        public int getExpectedAnswer() {
            return expectedAnswer;
        }

        public String getQuestion() {
            return question;
        }

        public List<Answer> getAnswers() {
            return answers;
        }

        public String getProperAnswer() {
            return answers.get(expectedAnswer - 1).getAnswer();
        }
    }

    /**
     * The answer itself is again a class.
     *
     * This is a little over-engineered, it might as well be a string.
     * Implementing it as a separate class enables you to add further 
     *   information - e.g. a scoring for the wrong / right answer...
     */
    private class Answer {

        private String answer;

        public Answer(String answer) {
            this.answer = answer;
        }

        public String getAnswer() {
            return answer;
        }

    }

}

如果您对实施有任何疑问,请随时在评论中提问我。快乐的编码!

答案 2 :(得分:0)

您可以在字符串中放置一个行分隔符。要确保程序在任何平台上都能正常运行,您应该使用%n格式化程序和printf字符串:

String[] questions = new String[] {
    // Question #1 with its answers
    "What is your name?%n" +
    "%n"                   +
    "1. Stack%n"           +
    "2. Overflow%n"        +
    "3. John%n"            +
    "4. Doe%n"

    // More questions here..
};

for (String question : questions) {
    System.out.printf(question);
}

答案 3 :(得分:0)

基本上,您需要做的就是在字符串中添加一个分隔字符,并在显示问题时对其进行评估。

String question = "Question\0First Answer\0Second Answer\n with Linebreak\0Third Answer";

分开使用split()

String[] questionAndAnswers = question.split("\0");

但这不是应该如何运作的。您应该创建一个Question对象,其中包含问题和可能答案的属性。然后构建一个Question个对象而不是String

public class Question {
     private String question;
     private String[] answers;
     private int correct;

     // have getters and setters here 
}