附加字符串的怪异行为

时间:2018-09-06 19:12:02

标签: java string string-concatenation

我正在使用以下代码来生成对象的文本转储:

public static String generateText(Quiz quiz){
    String endResult = "";
    for (QuizQuestion question : quiz.getQuestions()) {
        endResult += question.getQuestion();
        endResult += System.lineSeparator();
        endResult += question.getAnswer1();
        endResult += System.lineSeparator();
        endResult += question.getAnswer2();
        endResult += System.lineSeparator();
        endResult += question.getAnswer3();
        endResult += System.lineSeparator();
        endResult += question.getAnswer4();
        endResult += System.lineSeparator();
        endResult += question.getCorrectAnswer();
        endResult += System.lineSeparator();
    }

    return endResult;
}

但是,当我将endResult打印到文件tis时,输出是:

questiona1a2a3a4a

然后它打印4空行。如何解决?

0 个答案:

没有答案