在Java中,可以使用對象來表示問卷表結構。首先,創建一個Question類來表示問題,該類可以包含問題的內容、選項列表、答案等屬性。然后,創建一個Questionnaire類來表示問卷表,該類包含問題列表、問卷名稱、創建時間等屬性。
下面是一個示例代碼:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Question {
private String content;
private List<String> options;
private String answer;
public Question(String content, List<String> options, String answer) {
this.content = content;
this.options = options;
this.answer = answer;
}
// getters and setters
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<String> getOptions() {
return options;
}
public void setOptions(List<String> options) {
this.options = options;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
public class Questionnaire {
private String name;
private Date createDate;
private List<Question> questions;
public Questionnaire(String name) {
this.name = name;
this.createDate = new Date();
this.questions = new ArrayList<>();
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public List<Question> getQuestions() {
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
}
public class Main {
public static void main(String[] args) {
// 創建問卷表
Questionnaire questionnaire = new Questionnaire("Java基礎知識調查問卷");
// 創建問題
Question question1 = new Question("Java中的基本數據類型有哪些?",
List.of("int", "double", "boolean", "char"), "int");
Question question2 = new Question("Java中的循環語句有哪些?",
List.of("for", "while", "do-while", "if-else"), "for");
// 添加問題到問卷表
questionnaire.getQuestions().add(question1);
questionnaire.getQuestions().add(question2);
// 輸出問卷表內容
System.out.println("問卷名稱:" + questionnaire.getName());
System.out.println("創建時間:" + questionnaire.getCreateDate());
for (Question question : questionnaire.getQuestions()) {
System.out.println("問題:" + question.getContent());
System.out.println("選項:" + question.getOptions());
System.out.println("答案:" + question.getAnswer());
System.out.println();
}
}
}
以上代碼創建了一個問卷表,并包含了兩個問題。可以根據實際需求,添加更多的問題和選項。