为学校项目创建测验,如果在数组的一个元素中有一个问题和4个可能的答案,并且这个问题和4个可能的答案在他们的答案中自己的路线。有没有办法在Java的一个数组元素中有多行文本?
回答
你可以在你的字符串里放一个分隔符。为了确保该程序在任何平台上运行良好,你应该使用%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);
}
基本上,所有你需要做在你的字符串是增加伊朗式分居性质及计算上显示题。
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
}
如果创建String[]
(一个String
对象数组),那么该阵列中的每个元素可以包含任何有效的Java String
,其包括含有换行符\n
字符的字符串。因此,您可以使每个数组元素为多行字符串,每行用\n
字符分隔,以便在第一个\n
之前发现问题,在第二个\n
之前发现正确答案,并且发现错误答案后续的“线路”。
但是,我会说这实际上使代码更神秘,因此难以遵循。更好的面向对象的方法是创建一个代表测验问题及其答案的新类。这个类的轮廓看起来是这样的:
class QuizQuestion {
String questionText;
String correctAnswer;
List<String> incorrectAnswers;
}
你可以准确地离开这个班是这样,而且只是指它的类字段(来自同一代码包内),从而创建一个新的对象类型的很好地将问题文本和答案捆绑在一起。或者,如果需要,您可以向此类添加方法,以控制/保护类字段数据的访问/修改方式。
现在,您可以使用List<QuizQuestion>
或QuizQuestion[]
,而不是使用原来的String[]
。这应该使代码更易于阅读,并且在将来更容易增强。
尽管上面的所有答案都是针对您的问题的有效实现,但我宁愿采用面向对象的方法,正如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;
}
}
}
如果您有关于实施有关的任何问题随时问我的意见。快乐编码!
- 1. 有没有办法在一个数组
- 2. 有没有办法在一个数组
- 3. 有没有办法在jQuery中截断元素的文本?
- 4. 有没有办法在HTML中只显示一个纯文本元素?
- 5. 有没有办法断言一个元素没有类?
- 6. 有没有办法让元素在GTM
- 7. 有没有办法在一个Jsoup选择中获取多个元素?
- 8. 有没有办法在Java中无限循环地图元素?
- 9. 有没有办法让javax.xml有一个根元素包装?
- 10. 有没有办法过滤掉数组中出现多次的元素?
- 11. 有没有办法找出某个元素重叠的元素?
- 12. 有没有办法在两个数组中找到匹配元素?
- 13. 有没有办法检查数组中所有元素中的字符?
- 14. 有没有办法在一个if语句中有多个值?
- 15. 有没有办法从一个元素获得SlickGrid的实例
- 16. PVRTexTool,有没有办法一次运行多个文件?
- 17. 有没有办法在mongo文档中维护一个有序数组?
- 18. 有没有办法从一行中的函数返回一个数组键?
- 19. 有没有办法指定第一个元素* type *?
- 20. 有没有办法简单刷新一个元素?
- 21. 有没有办法让两个DataTemplates共享一些元素?
- 22. 有没有办法检查一个phpspec数组是否在多维数组中有一个键
- 23. 有没有办法获取C中的数组元素的数量?
- 24. 有没有办法在Java中并行拆分和连接多个数据?
- 25. 有没有办法搜索集合中的每个元素c#
- 26. 有没有办法来检查是否有一个字符串数组的下一个元素?
- 27. 有没有办法获得数组中元素出现的最高次数?
- 28. 有没有办法限制PowerShell中多文本框中的最大行数?
- 29. 有没有办法在MongoDB中将两个数组合并成一个数组?
- 30. 有没有办法在scala中选择一个数组中的所有东西?
你问如何制作多行字符串? – Mureinik
基本上是的,这将全部坐在单数阵列元素内 –
您似乎完全忽略了Java是OO语言的事实。不要创建一个字符串数组,其中每个字符串都必须被解析以将问题与其答案分开。例如,创建一个Question对象的数组,其中一个Question对象具有一个字段问题,或者输入String,以及一个String []类型的字段possibleAnswers。 –