By GokiSoft.com| 22:57 29/10/2021|
Java Advanced

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

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 overview.lession3;

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 Diep.Tran
 */
public class Main {
    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();
                    break;
                case 2:
                    showGame();
                    break;
                case 3:
                    System.out.println("Thoat Game!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 3);
    }
    
    static void showGame() {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        
        List<Question> questions = new ArrayList<>();
        
        try {
            fis = new FileInputStream("questions.dat");
            ois = new ObjectInputStream(fis);
            
            for(;;) {
                Object obj = null;
                try {
                    obj = ois.readObject();
                } catch(Exception e) {
                    obj = null;
                }
                if(obj == null) {
                    break;
                }
                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);
        } finally {
            if(ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        //du lieu cau hoi
        int correct = 0;
        int total = questions.size();
        
        //sinh ngau nhien 15 cau hoi.
        if(questions.size() > 15) {
            List<Question> testList = new ArrayList<>();
            Random random = new Random();
            
            for (int i = 0; i < 15; i++) {
                int index = random.nextInt(questions.size());
                testList.add(questions.remove(index));
            }
            
            questions = testList;
        }
        
        for (Question question : questions) {
            question.showQuestion();
            int result = Integer.parseInt(scan.nextLine());
            if(question.checkResult(result)) {
                correct++;
            }
        }
        
        System.out.format("\nKet qua test : %d/%d\n", correct, total);
    }
    
    static void inputQuestions() {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        
        try {
            fos = new FileOutputStream("questions.dat", true);
            oos = new ObjectOutputStream(fos);
            
            for(;;) {
                Question question = new Question();
                question.input();
                
                oos.writeObject(question);
                
                System.out.println("Tiep tuc nhap cau hoi khong Y/N:");
                String option = scan.nextLine();
                if(option.equalsIgnoreCase("N")) {
                    break;
                }
            }
        } 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(oos != null) {
                try {
                    oos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void showMenu() {
        System.out.println("1. Nhap cau hoi");
        System.out.println("2. Game");
        System.out.println("3. Thoat");
        System.out.println("Lua chon: ");
    }
}


#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 overview.lession3;

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

/**
 *
 * @author Diep.Tran
 */
public class Question implements Serializable{
    String title;
    List<String> options = new ArrayList<>();
    int result;

    public Question() {
    }

    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 getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap cau hoi: ");
        title = scan.nextLine();
        
        System.out.println("Nhap options: ");
        int index = 1;
        for(;;) {
            System.out.format("\nOption %d: ", index++);
            String option = scan.nextLine();
            options.add(option);
            
            System.out.println("Tiep tuc nhap hay ko Y/N: ");
            option = scan.nextLine();
            
            if(option.equalsIgnoreCase("N")) {
                break;
            }
        }
        System.out.println("Nhap dap an dung: ");
        result = Integer.parseInt(scan.nextLine());
    }
    
    public void showQuestion() {
        System.out.println("Question: " + title);
        int index = 1;
        for (String option : options) {
            System.out.println((index++) + ". " + option);
        }
        System.out.println("Dap an: ");
    }
    
    public boolean checkResult(int result) {
        return this.result == result;
    }
}


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

5

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