gpt4 book ai didi

java - 如何使用数组来存储问题和答案以进行测验

转载 作者:行者123 更新时间:2023-12-01 09:07:41 24 4
gpt4 key购买 nike

我正在做一项作业,该作业基于我的 Java 教科书中有关数组的一章,内容如下:

Use the Question class from Chapter 5 to define a Quiz class. A quiz can be composed of up to 25 questions. Define the add method of the Quiz class to add a question to a quiz. Define the giveQuiz method of the Quiz class to add a question to a quiz. Define the giveQuiz method of the Quiz class to present each question in turn to the user, accept an answer for each one, and keep track of the results. Define a class called QuizTime with a main method that chooses questions for a quiz, presents the quiz to the user, collects and checks the answers, and prints the final results.

我正在考虑的这项作业的程序开发计划可能正确,也可能不正确,如下:

  1. Create Quiz class

  2. create add method in Quiz class to add a question to a quiz

  3. Define giveQuiz method in Quiz class to give question to user

  4. make it accept answer for each question,

5.Keep track of results of answers

6.Create another class called QuizTime

  1. Create a main method.

8.Make method that allows questions to be inputted as an array into parameters of method

9.store and check answers

  1. print results.

在我看来,它要求总共创建 3 个类和一个主要方法。

但是,我对为什么需要这么多类(class)感到困惑。下面给出的 Question 类中的方法似乎具有执行赋值告诉程序员执行的操作的方法,但这些方法看起来是空的并且没有任何用处。我对 #6-10 的程序开发计划也感到困惑。我不确定如何创建一个数组来将问题和答案存储到方法中。

这是 Java 教科书中已经给出的 Question 类:

    //********************************************************************
// Question.java Author: Lewis/Loftus/Cocking
//
// Represents a question (and its answer).
//********************************************************************

public class Question implements Complexity
{
private String question, answer;
private int complexityLevel;

//-----------------------------------------------------------------
// Sets up the question with a default complexity.
//-----------------------------------------------------------------
public Question (String query, String result)
{
question = query;
answer = result;
complexityLevel = 1;
}

//-----------------------------------------------------------------
// Sets the complexity level for this question.
//-----------------------------------------------------------------
public void setComplexity (int level)
{
complexityLevel = level;
}

//-----------------------------------------------------------------
// Returns the complexity level for this question.
//-----------------------------------------------------------------
public int getComplexity()
{
return complexityLevel;
}

//-----------------------------------------------------------------
// Returns the question.
//-----------------------------------------------------------------
public String getQuestion()
{
return question;
}
//-----------------------------------------------------------------
// Returns the answer to this question.
//-----------------------------------------------------------------
public String getAnswer()
{
return answer;
}

//-----------------------------------------------------------------
// Returns true if the candidate answer matches the answer.
//-----------------------------------------------------------------
public boolean answerCorrect (String candidateAnswer)
{
return answer.equals(candidateAnswer);
}

//-----------------------------------------------------------------
// Returns this question (and its answer) as a string.
//-----------------------------------------------------------------
public String toString()
{
return question + "\n" + answer;
}
}

Here is the quiz class that I created in which I haven't worked on much.

public class Quiz
{
private String add;

public String addQuest (String addQ)//adds questions to quiz
{
add = addQ;
return add;//returninng the question
}

public String giveQuiz ()//give the quiz to the user
{
//accept and store each answer
return;
}
}

测验时间类(class):

public class QuizTime
{
private String question;

public String[] Quiz() {
}
public String toString(String quest)
{
question = quest;
for (int i = 0; i < 25; i++)
Quiz[i] = new Quiz (question);
}

public String presents ()
{
for (int i = 0; i < 25; i++)
return quest[i];
}
}

我还没有制作主要方法,因为我仍然不知道这些类的开发将如何进行。我为我在类和数组方面的低水平道歉,因为我刚刚学习了它们。我非常感谢你们提前提供的帮助。

对于 Quiz 类,在第一个方法中,我尝试让主方法插入可以存储在方法内的问题的参数。但我想知道是否有一种更简单的方法可以在方法内部存储测验问题的 25 个参数(如果可能的话)。我正在尝试请让我知道我是否走在正确的轨道上。非常感谢。

最佳答案

But I want to know if there is an easier way to store 25 parameters of quiz questions inside of the method if that is even possible

测验问题没有必要有 25 个参数。这就是为什么您应该使用学校的数组。

它看起来像这样:

public Question[] getQuestions(){
return questions;
}

出于任何原因,如果您需要通过某种方法传递所有问题,它将如下所示:

public void sendQuestions(Question[] ques){
//do whatever
}

关于java - 如何使用数组来存储问题和答案以进行测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41132815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com