学生类:

public class Student {
    private String name;
    private int age;
    private String gender;
    private String id;

    public Student(String name, int age, String gender, String id) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getGender() {
        return gender;
    }

    public String getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void study() {
        System.out.println(name + " is studying.");
    }
}

老师类:

import java.util.ArrayList;

public class Teacher {
    private String name;
    private ArrayList<String> questions;
    private ArrayList<Integer> likes;

    public Teacher(String name) {
        this.name = name;
        this.questions = new ArrayList<String>();
        this.likes = new ArrayList<Integer>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void askQuestion(String question) {
        questions.add(question);
        likes.add(0);
    }

    public void likeQuestion(int index) {
        likes.set(index, likes.get(index) + 1);
    }

    public void commentOnQuestion(int index, String comment) {
        String question = questions.get(index);
        question += "\nComment: " + comment;
        questions.set(index, question);
    }

    public void displayQuestions() {
        for (int i = 0; i < questions.size(); i++) {
            System.out.println("Question " + (i + 1) + ": " + questions.get(i));
            System.out.println("Likes: " + likes.get(i));
            System.out.println();
        }
    }
}

提问者的在线问答点赞评论系统:

import java.util.Scanner;

public class QnA {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Teacher teacher = new Teacher("Mr. Smith");

        boolean running = true;

        while (running) {
            System.out.println("1. Ask a question");
            System.out.println("2. Like a question");
            System.out.println("3. Comment on a question");
            System.out.println("4. Display questions");
            System.out.println("5. Exit");

            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("Enter your question:");
                    scanner.nextLine();
                    String question = scanner.nextLine();
                    teacher.askQuestion(question);
                    System.out.println("Question added.");
                    break;
                case 2:
                    System.out.println("Enter the index of the question you want to like:");
                    int index = scanner.nextInt() - 1;
                    teacher.likeQuestion(index);
                    System.out.println("Question liked.");
                    break;
                case 3:
                    System.out.println("Enter the index of the question you want to comment on:");
                    index = scanner.nextInt() - 1;
                    System.out.println("Enter your comment:");
                    scanner.nextLine();
                    String comment = scanner.nextLine();
                    teacher.commentOnQuestion(index, comment);
                    System.out.println("Comment added.");
                    break;
                case 4:
                    teacher.displayQuestions();
                    break;
                case 5:
                    running = false;
                    break;
                default:
                    System.out.println("Invalid choice.");
                    break;
            }
        }
    }
}
编写一段java代码完成一个学生类。老师类 、提问者的在线问答点赞评论系统

原文地址: https://www.cveoy.top/t/topic/b9jV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录