2017-01-02 125 views
2

爲學校項目創建測驗,如果在數組的一個元素中有一個問題和4個可能的答案,並且這個問題和4個可能的答案在他們的答案中自己的路線。有沒有辦法在Java的一個數組元素中有多行文本?

+0

你問如何製作多行字符串? – Mureinik

+0

基本上是的,這將全部坐在單數陣列元素內 –

+4

您似乎完全忽略了Java是OO語言的事實。不要創建一個字符串數組,其中每個字符串都必須被解析以將問題與其答案分開。例如,創建一個Question對象的數組,其中一個Question對象具有一個字段問題,或者輸入String,以及一個String []類型的字段possibleAnswers。 –

回答

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); 
} 
+1

@hardillb'%n'是一個用於'printf'的平臺中立格式化程序,用正確的換行符替換。 – Mureinik

+0

你確定你必須使用'+'嗎?我認爲它必須是','而不是'+'... –

+0

@Mohsen_Fatemi這是**數組**的一個元素,分解爲多個字符串以便於閱讀。你應該在數組的**不同**元素之間使用','。 – Mureinik

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 
} 
1

如果創建String[](一個String對象數組),那麼該陣列中的每個元素可以包含任何有效的Java String,其包括含有換行符\n字符的字符串。因此,您可以使每個數組元素爲多行字符串,每行用\n字符分隔,以便在第一個\n之前發現問題,在第二個\n之前發現正確答案,並且發現錯誤答案後續的「線路」。

但是,我會說這實際上使代碼更神祕,因此難以遵循。更好的面向對象的方法是創建一個代表測驗問題及其答案的新類。這個類的輪廓看起來是這樣的:

class QuizQuestion { 
    String questionText; 
    String correctAnswer; 
    List<String> incorrectAnswers; 
} 

你可以準確地離開這個班是這樣,而且只是指它的類字段(來自同一代碼包內),從而創建一個新的對象類型的很好地將問題文本和答案捆綁在一起。或者,如果需要,您可以向此類添加方法,以控制/保護類字段數據的訪問/修改方式。

現在,您可以使用List<QuizQuestion>QuizQuestion[],而不是使用原來的String[]。這應該使代碼更易於閱讀,並且在將來更容易增強。

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; 
     } 

    } 

} 

如果您有關於實施有關的任何問題隨時問我的意見。快樂編碼!

相關問題