2017-01-02 92 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; 
     } 

    } 

} 

如果您有关于实施有关的任何问题随时问我的意见。快乐编码!

相关问题