Java

java怎么自定义问卷表结构

小亿
116
2023-11-08 19:43:39
栏目: 编程语言

在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();
        }
    }
}

以上代码创建了一个问卷表,并包含了两个问题。可以根据实际需求,添加更多的问题和选项。

0
看了该问题的人还看了