爲學校項目創建測驗,如果在數組的一個元素中有一個問題和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. 有沒有辦法從函數返回的數組中獲取一個元素?
- 5. 有沒有辦法在HTML中只顯示一個純文本元素?
- 6. 有沒有辦法斷言一個元素沒有類?
- 7. 有沒有辦法將一組元素添加到表單中?
- 8. 有沒有辦法讓元素在GTM
- 9. 有沒有辦法在一個Jsoup選擇中獲取多個元素?
- 10. 有沒有辦法在Java中無限循環地圖元素?
- 11. 有沒有辦法找出隊列中有多少元素
- 12. 有沒有辦法讓javax.xml有一個根元素包裝?
- 13. 有沒有辦法引用多個類的html元素?
- 14. 有沒有辦法過濾掉數組中出現多次的元素?
- 15. 有沒有辦法找出某個元素重疊的元素?
- 16. 有沒有辦法在WSDL中使用本地xsd元素
- 17. 有沒有辦法在兩個數組中找到匹配元素?
- 18. 有沒有辦法獲得python/selenium中另一個元素的元素?
- 19. 有沒有辦法檢查數組中所有元素中的字符?
- 20. 有沒有辦法在一行中寫javascript對象數組?
- 21. 有沒有辦法在一個if語句中有多個值?
- 22. 有沒有辦法只從一個數組的MongoDB文檔
- 23. 有沒有辦法在IPython筆記本中同時運行多個單元?
- 24. 有沒有辦法選擇一個不是小孩的元素?
- 25. 有沒有辦法從一個元素獲得SlickGrid的實例
- 26. 有沒有辦法限制一個HTML元素的CSS類?
- 27. 有沒有辦法選擇一個類的特定元素?
- 28. 在Eclipse中有沒有辦法讓多個數組中的值保持一致?
- 29. PVRTexTool,有沒有辦法一次運行多個文件?
- 30. 有沒有辦法在java中獲取數組的名稱?
你問如何製作多行字符串? – Mureinik
基本上是的,這將全部坐在單數陣列元素內 –
您似乎完全忽略了Java是OO語言的事實。不要創建一個字符串數組,其中每個字符串都必須被解析以將問題與其答案分開。例如,創建一個Question對象的數組,其中一個Question對象具有一個字段問題,或者輸入String,以及一個String []類型的字段possibleAnswers。 –