By GokiSoft.com| 19:10 25/07/2022|
Java Advanced

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

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


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt1376;

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.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 QTA
 */
public class Main {
    static Scanner scan = new Scanner(System.in);
    static List<Question> questions = new ArrayList<>();
    
    public static void main(String[] args) {
        int choose;
        readData();
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    input();
                    break;
                case 2:
                    gamePlaying();
                    break;
                case 3:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 3);
    }
    
    static void showMenu() {
        System.out.println("1. Nhap du lieu");
        System.out.println("2. Tham gia thi");
        System.out.println("3. Thoat");
        System.out.println("Chon: ");
    }

    private static void input() {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        
        System.out.println("Nhap so cau hoi: ");
        int N = Integer.parseInt(scan.nextLine());
        
        try {
            fos = new FileOutputStream("questions.dat", true);
            oos = new ObjectOutputStream(fos);
            
            for (int i = 0; i < N; i++) {
                Question q = new Question();
                q.input();
                
                oos.writeObject(q);
                
                questions.add(q);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(oos != null) {
                try {
                    oos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    private static void readData() {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        
        try {
            fis = new FileInputStream("questions.dat");
            ois = new ObjectInputStream(fis);
            
            Object obj;
            while((obj = ois.readObject()) != null) {
                questions.add((Question) obj);
            }
        } catch (FileNotFoundException ex) {
//            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
//            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private static void gamePlaying() {
        List<Question> quizList = new ArrayList<>();
        
        //questions -> rat nhieu cau hoi
        //lay ngau nhien ra 15 cau hoi -> Quiz
        if(questions.size() > 15) {
            List<Integer> randList = new ArrayList<>();
            for (int i = 0; i < questions.size(); i++) {
                randList.add(i);
            }

            Random random = new Random();
            //Lay ra 15 cau hoi
            for (int i = 0; i < 15; i++) {
                int index = random.nextInt(randList.size());
                quizList.add(questions.get(randList.get(index)));

                randList.remove(index);
            }
        } else {
            quizList = questions;
        }
        
        System.out.println("Bat dau game: ");
        int total = quizList.size();
        int correctNum = 0;
        for (Question question : quizList) {
            question.quiz();
            
            if(question.correct()) {
                correctNum++;
            }
        }
        
        System.out.println("Ket qua: " + correctNum + "/" + total);
    }
}


#Question.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt1376;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author QTA
 */
public class Question implements Serializable{
    String title;
    List<String> optionList;
    int index;
    int answerIndex;

    public Question() {
        optionList = new ArrayList<>();
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap tieu de cau hoi: ");
        title = scan.nextLine();
        
        String exit;
        do {
            System.out.println("Nhap lua chon: ");
            String option = scan.nextLine();
            optionList.add(option);
            System.out.println("Tiep tuc nhap hay ko y/N?");
            exit = scan.nextLine();
        } while(!exit.equalsIgnoreCase("N"));
        
        System.out.println("Nhap vi tri dap an chinh xac: ");
        index = Integer.parseInt(scan.nextLine());
    }
    
    public void quiz() {
        System.out.println(title);
        for (int i = 0; i < optionList.size(); i++) {
            System.out.println((i + 1)+". "+optionList.get(i));
        }
        System.out.println("Chon dap an: ");
        Scanner scan = new Scanner(System.in);
        answerIndex = Integer.parseInt(scan.nextLine());
    }
    
    public boolean correct() {
        return index == answerIndex;
    }

    public String getTitle() {
        return title;
    }

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

    public List<String> getOptionList() {
        return optionList;
    }

    public void setOptionList(List<String> optionList) {
        this.optionList = optionList;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getAnswerIndex() {
        return answerIndex;
    }

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


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 đó