【问题标题】:How do I display the Questions of this Quiz?如何显示此测验的问题?
【发布时间】:2016-12-07 22:19:02
【问题描述】:

所以我正在创建一个包含 25 个问题的测验,但是当我运行我的驱动程序课程时,问题没有出现。只有框供用户输入他们的答案。我应该怎么做才能解决这个问题?

public class Question implements Complexity // defines the class as Question, implements the interface Complexity
{ // begins class
  private String question; // sets question as a String that is only accessible in certain areas
  private String answer; // sets answer as a String that is only accessible in certain areas
  private int complexityLevel; // sets complexityLevel as an int to determine the complexity level, only accessible in certain areas
//----------------------------------------------------------------
//  Sets up the question with a default complexity. 
//----------------------------------------------------------------
  public Question (String query, String result) // sets Question as a constructor to create a question and sets complexitylevel to 1
  { // begins block
    question = query; // sets question equal to query 
    answer = result; // sets answer equal to result
    complexityLevel = 1; // sets the default complexity level to 1
  } // ends block
//----------------------------------------------------------------
//  Sets the complexity level for this question. 
//----------------------------------------------------------------
  public void setComplexity (int level) // sets setComplexity as a public void to set the complexitylevel of a question
  { // begins block
    complexityLevel = level; // sets complexityLevel equal to level
  } // ends block
//----------------------------------------------------------------
//  Returns the complexity level for this question. 
//----------------------------------------------------------------
  public int getComplexity() // sets getComplexity as an int to return the complexity level 
  { // begins block
    return complexityLevel; // returns the complexity level 
  } // ends block
//----------------------------------------------------------------
//  Returns the question. 
//----------------------------------------------------------------
  public String getQuestion() // set getQuestion as a public String to return the question 
  { // begins block
    return question; // returns the question 
  } // ends block
//----------------------------------------------------------------
//  Returns the answer to this question. 
//----------------------------------------------------------------
  public String getAnswer() // sets getAnswer as a public String to return the answer to the question
  { // begins block
    return answer; // returns the answer
  } // ends block
//----------------------------------------------------------------
//  Returns true if the candidate answer matches the answer. 
//----------------------------------------------------------------
  public boolean answerCorrect (String candidateAnswer) // sets answerCorrect as a public boolean to return true if the user's answer equals the question 
  { // begins block
    return answer.equals(candidateAnswer); // returns true 
  } // ends block
//----------------------------------------------------------------
//  Returns this question (and its answer) as a string. 
//----------------------------------------------------------------
  public String toString() // sets toString as a String to return the question and answer as a String 
  { // begins block
    return question + "\n" + answer; // returns the question and answer as a String 
  } // ends block
} // ends class

测验类:

import java.util.Scanner;
public class Quiz
{
  private int score;
  private Question[] questionHolder = new Question[25];
  private int numQuestions;

  public Quiz()
  {
    this.score = 0;
    this.numQuestions = 0;  
  }

  public void addQuestion (Question Q)
  {
    this.questionHolder[numQuestions++] = Q;
  }

  public int giveQuiz() 
  {
    Scanner scan = new Scanner (System.in);
    String candidateAnswer;
    scan.nextLine();
    for (int i = 0; i < numQuestions; i++)
    {
      candidateAnswer = scan.nextLine();
      if (questionHolder[i].answerCorrect(candidateAnswer))
        score++;
    }
    return getscore();
  }

  public int getscore()
  {
    return score;
  }

  public String toString()
  {
    return getscore() + "\n";
  } 
}

驱动类:

public class QuizTime
{
//--------------------------------------------------------------------------
//  Creates the question and answer.
//--------------------------------------------------------------------------
public static void main (String[] args)
{
Quiz T1;

Question Q1 = new Question ("What is the capital of Virginia?", "Richmond");

Question Q2 = new Question ("Is an apple a Fruit or a vegetable?", "Fruit");

Question Q3 = new Question ("What continent is China in?", "Asia");

Question Q4 = new Question ("Is Germany in Europe or South America?", "Europe");

Question Q5 = new Question ("What color is a black bear?", "Black");

Question Q6 = new Question ("What is the capital of Arizona?", "Phoenix");

Question Q7 = new Question ("What do cows produce??", "Milk");

Question Q8 = new Question ("What ocean is closest to New York City?", "Atlantic");

Question Q9 = new Question ("What ocean surrounds Japan?", "Pacific");

Question Q10 = new Question ("What is the largest state in America?", "Alaska");

Question Q11 = new Question ("What is the smallest state?", "Deleware");

Question Q12 = new Question ("What is the most populated state?", "California");

Question Q13 = new Question ("What is instrument did Jascha Heifetz play?", "Violin");

Question Q14 = new Question ("Was Mozart a composer or a computer?", "Composer");

Question Q15 = new Question ("What is the largest country by area?", "Russia");

Question Q16 = new Question ("What is the most populated country?", "China");

Question Q17 = new Question ("What country did Pizza originate in?", "Italy");

Question Q18 = new Question ("What is the last name of the first American President?", "Washington");

Question Q19 = new Question ("What country borders America to the south?", "Mexico");

Question Q20 = new Question ("What island is 700 miles off the coast of NYC?", "Bermuda");

Question Q21 = new Question ("What city contains the Eiffel Tower?", "Paris");

Question Q22 = new Question ("Who wrote Romeo and Juliet?", "Shakespeare");

Question Q23 = new Question ("What swims in the ocean?", "Fish");

Question Q24 = new Question ("What is man's best friend?", "Dog");

Question Q25 = new Question ("What is another name for coffee and the language of this program?", "Java");


//--------------------------------------------------------------
//Adds the questions into quiz.
//--------------------------------------------------------------
T1= new Quiz();
T1.addQuestion(Q1);
T1.addQuestion(Q2);
T1.addQuestion(Q3);
T1.addQuestion(Q4);
T1.addQuestion(Q5);
T1.addQuestion(Q6);
T1.addQuestion(Q7);
T1.addQuestion(Q8);
T1.addQuestion(Q9);
T1.addQuestion(Q10);
T1.addQuestion(Q11);
T1.addQuestion(Q12);
T1.addQuestion(Q13);
T1.addQuestion(Q14);
T1.addQuestion(Q15);
T1.addQuestion(Q16);
T1.addQuestion(Q17);
T1.addQuestion(Q18);
T1.addQuestion(Q19);
T1.addQuestion(Q20);
T1.addQuestion(Q21);
T1.addQuestion(Q22);
T1.addQuestion(Q23);
T1.addQuestion(Q24);
T1.addQuestion(Q25);
//--------------------------------------------------------------
//  Prints out the quiz.
//--------------------------------------------------------------
System.out.println(T1.giveQuiz());
}
}

【问题讨论】:

    标签: arrays add


    【解决方案1】:

    我真的希望我能发表评论,但我没有看到您显示问题的代码。不过,我知道您在哪里接受用户输入。

    for (int i = 0; i < numQuestions; i++)
    {
      candidateAnswer = scan.nextLine();
      if (questionHolder[i].answerCorrect(candidateAnswer))
        score++;
    }
    

    注意:今后,如果您正在与他人一起开展项目,请尽量避免添加美化文字。它占用空间并且不传达任何额外信息。尤其是这么短的方法,你不需要添加开始和结束标签。

    【讨论】:

    • 问题在我的司机课 QuizTime 中。
    • 这是您创建问题并将问题添加到列表的地方。正如@sedrick 所说,您需要在 for 循环内向giveQuiz() 添加一行代码。他的解决方案应该可以解决您的问题。
    • 确实如此。谢谢
    【解决方案2】:

    在这段代码里面,我想你需要添加
    System.out.println(questionHolder[i].toString());
    scan.nextLine();之前

          public int giveQuiz() 
          {
            Scanner scan = new Scanner (System.in);
            String candidateAnswer;
            scan.nextLine();
            for (int i = 0; i < numQuestions; i++)
            {
              candidateAnswer = scan.nextLine();
              if (questionHolder[i].answerCorrect(candidateAnswer))
                score++;
            }
            return getscore();
          }
    

    除了Scanner scan = new Scanner(System.in);、return getscore(); 和String candidateAnswer; 之外,您在此方法中的所有代码似乎都应该在 for 循环中

    类似:

              public int giveQuiz() 
              {
                Scanner scan = new Scanner (System.in);
                String candidateAnswer;
    
                for (int i = 0; i < numQuestions; i++)
                {
                  System.out.println(questionHolder[i].toString()); 
                  candidateAnswer = scan.nextLine();
                  if (questionHolder[i].answerCorrect(candidateAnswer))
                    score++;
                }
                return getscore();
              }
    

    【讨论】:

    • 它现在确实显示了问题,但它也显示了答案。如何隐藏答案?
    • 在您的问题类中,从 toString() 方法中删除答案。
    • 或者代替 questionHolder[i].toString();使用 questionHolder[i].getQuestion();
    猜你喜欢
    • 2021-10-10
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 2021-03-09
    • 2022-09-28
    相关资源
    最近更新 更多