By GokiSoft.com| 14:43 17/07/2023|
Java Advanced

[Share Code] Java Basic - Phần mềm AI LÀ TRIỆU PHÚ - C2209I

Java Basic - Phần mềm AI LÀ TRIỆU PHÚ

#BT1376.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.java2.lesson04;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author teacher
 */
public class BT1376 {

    static class Question implements Serializable {

        String title;
        //options: ["Option 1", "Option 2", "Option 3"] => Index: 0, 1, 2
        List<String> options;
        //answerIndex: 1, 2, 3 -> Nguoi dung
        int answerIndex;

        public Question() {
            options = new ArrayList<>();
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public List<String> getOptions() {
            return options;
        }

        public void setOptions(List<String> options) {
            this.options = options;
        }

        public int getAnswerIndex() {
            return answerIndex;
        }

        public void setAnswerIndex(int answerIndex) {
            this.answerIndex = answerIndex;
        }

        public boolean showQuestion() {
            System.out.println("=== CAU HOI: " + title);
            for (int i = 0; i < options.size(); i++) {
                System.out.println((i + 1) + ". " + options.get(i));
            }
            System.out.println("Dap an: ");
            Scanner scan = new Scanner(System.in);
            int choose = -1;
            try {
                choose = Integer.parseInt(scan.nextLine());
            } catch (NumberFormatException e) {
            }
            return choose == answerIndex;
        }

        public void input() {
            System.out.println("=== CAU HOI ===");
            Scanner scan = new Scanner(System.in);
            System.out.println("Nhap cau hoi: ");
            title = scan.nextLine();
            do {
                System.out.println("Nhap dap an: ");
                String opt = scan.nextLine();
                if (opt.isEmpty() || opt.equalsIgnoreCase("N")) {
                    break;
                }
                options.add(opt);
            } while (true);
            System.out.println("Nhap dap an dung: ");
            do {
                try {
                    answerIndex = Integer.parseInt(scan.nextLine());
                    if (answerIndex <= options.size() && answerIndex > 0) {
                        break;
                    }
                } catch (NumberFormatException e) {
                }
                System.out.println("Nhap lai dap an dung: ");
            } while (true);
        }
    }

    static List<Question> questions = new ArrayList<>();
    static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        int choose;

        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1 ->
                    inputQuestions();
                case 2 ->
                    playGaming();
                case 3 ->
                    System.out.println("Thoat!!!");
                default ->
                    System.out.println("Nhap sai!!!");
            }
        } while (choose != 3);
    }

    static void showMenu() {
        System.out.println("1. Nhap cau hoi");
        System.out.println("2. Play gaming");
        System.out.println("3. Thoat");
        System.out.println("Chon: ");
    }

    private static void inputQuestions() {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;

        try {
            fos = new FileOutputStream("questions.dat", true);
            oos = new ObjectOutputStream(fos);

            String continueStr;
            do {
                Question question = new Question();
                question.input();
                oos.writeObject(question);

                System.out.println("Ban co tiep tuc them cau hoi khong Y/n: ");
                continueStr = scan.nextLine();
            } while (!continueStr.equalsIgnoreCase("N"));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(BT1376.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(BT1376.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException ex) {
                    Logger.getLogger(BT1376.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(BT1376.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        System.out.println("FINISH");
    }

    private static List<Question> readFile() {
        List<Question> dataList = new ArrayList<>();

        FileInputStream fis = null;
        ObjectInputStream ois = null;

        try {
            fis = new FileInputStream("questions.dat");
            ois = new ObjectInputStream(fis);

            while (true) {
                try {
                    Object obj = ois.readObject();
                    if (obj == null) {
                        break;
                    }
                    Question q = (Question) obj;
                    dataList.add(q);
                } catch (IOException | ClassNotFoundException e) {
                    break;
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(BT1376.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(BT1376.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                    Logger.getLogger(BT1376.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(BT1376.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (dataList.size() <= 15) {
            return dataList;
        }

        //Lay ngau nhien ra 15 cau hoi trong dataList
        List<Question> questionList = new ArrayList<>();

        Random random = new Random();
        int rad;
        while (questionList.size() < 15) {
            rad = random.nextInt(dataList.size() - 1);
            Question q = dataList.remove(rad);
            questionList.add(q);
        }

        return questionList;
    }

    private static void playGaming() {
        List<Question> questionList = readFile();
        int correctCount = 0;
        int total = questionList.size();
        for (Question question : questionList) {
            boolean answer = question.showQuestion();
            if (answer) {
                correctCount++;
            }
        }

        System.out.println("=== KET QUA: " + correctCount + " / " + total);
    }
}
Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó