Mục Lục
Mục Lục
Exception & Collection Video Tìm hiểu exception + debug + package - lập trình Java Nâng Cao Video Tìm hiểu collections, Sets, Maps và Generic - lập trình java nâng cao Chương trình quản lý sinh viên bằng HashMap - Java Advanced Bài tập ôn luyện String, StringBuilder, StringBuffer - Java Advanced Bài tập Exception trong Java File Video Hướng dẫn đọc ghi file FileInputStream, FileOutputStream, ObjectInputStream trong java - lập trình java nâng cao File - Quản lý thông tin sinh viên Quản lý thông tin sinh & lưu object file - Java nâng cao Video Quản lý thông tin sinh & lưu object file - Java nâng cao Video Bài tập quản lý bán vé máy bay - java nâng cao Thread Video Tìm hiểu Thread trong Java - Phần 2 - lập trình java nâng cao Thread thực hiện in ngẫu nhiên các số nguyên và ký tự Video Thread thực hiện in ngẫu nhiên các số nguyên và ký tự - Phần 1 Video Thread thực hiện in ngẫu nhiên các số nguyên và ký tự & Đông bộ thread + wait notify + synchronized + Java nâng cao - Phần 2 Sử dụng MultiThreading sinh ký tự a-zA-Z trong java CSDL Video Kết nối CSDL bằng java swing phần 1 - lập trình java nâng cao Design Pattern & Đa Ngôn Ngữ Video Hướng dẫn tìm hiểu đệ quy qua bài Fibonaci - Recursion Fibonaci - Java Phân chia mảng số nguyên thành 2 phần + chắc + lẻ Video Phân chia mảng số nguyên thành 2 phần + chắc + lẻ Video Design Pattern lập trình java nâng cao - lập trình java nâng cao Bài tập - Chương trình quản lý sách - lập trình đa ngôn ngữ - Khoá học lập trình Java nâng cao XML & JSON & CSDL Video Tìm hiểu XML & Hướng dẫn phân tích tài liệu XML quản lý lớp học bằng Java XML: Viết chương trình phân tích tài liệu XML thông tin cá nhân bằng Java Video - Hướng dẫn tạo dự án quản lý sinh viên + import + export XML File XML: Java Swing|FX Quản lý thông tin cá nhân Profile bằng java - import + export XML File Video Json/Gson & Java - Phân tích dữ liệu lớp học JSON bằng Java -Ứng dụng quản lý lớp học Java nâng cao Video gson trong Java - Vi dụ json trong java - Bài tập quản lý sản phẩm + json trong Java JSON: Tạo JSON file & đọc nội dung JSON file thông tin cá nhân bằng Java Bài tập - Nhập thông tin sinh viên từ JSON vào CSDL - Lập trình Java nâng cao. Mini Project - Phân tích dự án + Phát triển phần mềm ATM Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java Video C1 > Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java Video C2 > Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java Bài tập - Viết chương trình quản lý tiêm chủng Vacxin COVID-19 - Lập trình Java nâng cao. Bài tập - Viết tools hỗ trợ đọc logs bằng Java Bài tập - Viết chương trình quản lý sinh viên XML - JSON - Lập trình Java Quản lý sinh viên + XML + JSON + MySQL bằng Java Video Quản lý sinh viên + XML + JSON + MySQL bằng Java Ôn Tập Tổng Quát & Exmination Bài tập luyện thi Java2 Bài thi Java 2
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;
    }
}




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

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

×