Java代码:创建一个调查问卷,里面能创建多个类型的问题
时间: 2024-04-05 16:32:37 浏览: 115
java开源问卷调查系统、表单系统
5星 · 资源好评率100%
好的,以下是一个简单的 Java 代码,可以创建一个调查问卷,里面可以包含多个类型的问题(单选题、多选题、填空题等)。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Survey {
private String title;
private ArrayList<Question> questions;
public Survey(String title) {
this.title = title;
this.questions = new ArrayList<>();
}
public void addQuestion(Question q) {
questions.add(q);
}
public void conductSurvey() {
System.out.println(title);
Scanner scanner = new Scanner(System.in);
for (Question q : questions) {
System.out.println(q.getQuestion());
if (q instanceof MultipleChoiceQuestion) {
MultipleChoiceQuestion mcq = (MultipleChoiceQuestion) q;
for (String option : mcq.getOptions()) {
System.out.println(option);
}
}
String answer = scanner.nextLine();
q.setAnswer(answer);
}
System.out.println("Survey completed. Thank you for your answers!");
}
}
```
这里定义了一个 `Survey` 类,表示一个调查问卷,包含标题和问题列表。`addQuestion` 方法用于向问卷中添加一个问题。`conductSurvey` 方法用于进行调查,逐个输出问题,并根据不同的问题类型,输出选项或提示用户输入答案。答案将保存在问题对象中,以便后续分析。
我们还需要定义一个 `Question` 类,作为问题的基类。
```java
public abstract class Question {
protected String question;
protected String answer;
public Question(String question) {
this.question = question;
}
public String getQuestion() {
return question;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
```
这里定义了一个抽象类 `Question`,包含问题和答案两个属性,以及相应的 getter 和 setter 方法。由于问题类型不同,答案的类型也会不同,因此这里将答案的类型定义为字符串,可以根据实际情况进行转换。
现在我们可以定义不同类型的问题,例如单选题和多选题。
```java
import java.util.ArrayList;
public class MultipleChoiceQuestion extends Question {
private ArrayList<String> options;
public MultipleChoiceQuestion(String question, ArrayList<String> options) {
super(question);
this.options = options;
}
public ArrayList<String> getOptions() {
return options;
}
}
public class CheckboxQuestion extends Question {
private ArrayList<String> options;
public CheckboxQuestion(String question, ArrayList<String> options) {
super(question);
this.options = options;
}
public ArrayList<String> getOptions() {
return options;
}
}
public class ShortAnswerQuestion extends Question {
public ShortAnswerQuestion(String question) {
super(question);
}
}
public class LongAnswerQuestion extends Question {
public LongAnswerQuestion(String question) {
super(question);
}
}
```
这里定义了四种类型的问题:单选题、多选题、填空题和长文本题。单选题和多选题需要提供选项列表,可以使用 `ArrayList` 来存储。
现在我们可以创建一个调查问卷,并添加一些问题。例如:
```java
Survey survey = new Survey("My Survey");
MultipleChoiceQuestion q1 = new MultipleChoiceQuestion("What is your favorite color?",
new ArrayList<String>(Arrays.asList("Red", "Green", "Blue")));
survey.addQuestion(q1);
CheckboxQuestion q2 = new CheckboxQuestion("Which of the following fruits do you like?",
new ArrayList<String>(Arrays.asList("Apple", "Banana", "Orange", "Strawberry")));
survey.addQuestion(q2);
ShortAnswerQuestion q3 = new ShortAnswerQuestion("What is your name?");
survey.addQuestion(q3);
LongAnswerQuestion q4 = new LongAnswerQuestion("What do you think about the current political situation?");
survey.addQuestion(q4);
```
最后调用 `conductSurvey` 方法即可开始调查。
```java
survey.conductSurvey();
```
这里只是一个简单的示例,实际使用中需要根据具体需求进行扩展和优化。
阅读全文