By GokiSoft.com| 14:34 04/11/2022|
Java Advanced

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

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

#Question.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 java2.lesson04;

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

/**
 *
 * @author diepvan
 */
public class Question implements Serializable{
    String title;
    List<String> options;
    int answer;

    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 getAnswer() {
        return answer;
    }

    public void setAnswer(int answer) {
        this.answer = answer;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("=========================");
        System.out.println("Nhap cau hoi: ");
        title = scan.nextLine();
        System.out.println("Nhap so lua chon: ");
        int n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            System.out.println("Lua chon " + (i+1));
            String opt = scan.nextLine();
            options.add(opt);
        }
        System.out.println("Chon dap an chinh xac: ");
        answer = Integer.parseInt(scan.nextLine());
        System.out.println("=========================");
    }
    
    public boolean playing() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Cau hoi: " + title);
        for (int i = 0; i < options.size(); i++) {
            System.out.println(i + ". " + options.get(i));
        }
        System.out.println("Chon: ");
        int anw = Integer.parseInt(scan.nextLine());
        
        if(anw == answer) return true;
        return false;
    }
}

#Test.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 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.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 diepvan
 */
public class Test {
    static Scanner scan;
    static List<Question> questionList;
    
    public static void main(String[] args) {
        scan = new Scanner(System.in);
        questionList = new ArrayList<>();
        
        readFile();
        
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch (choose) {
                case 1:
                    input();
                    break;
                case 2:
                    playingGame();
                    break;
                case 3:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 3);
        
    }
    
    static void readFile() {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        
        try {
            fis = new FileInputStream("game.dat");
            ois = new ObjectInputStream(fis);
            
            questionList = (List<Question>) ois.readObject();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        System.out.println("Finish!!!");
        
    }
    
    static void input() {
        System.out.println("Nhap so cau hoi can them: ");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            Question q = new Question();
            q.input();
            
            questionList.add(q);
        }
        
        //Luu du lieu vao File
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        
        try {
            fos = new FileOutputStream("game.dat");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(questionList);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(oos != null) {
                try {
                    oos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void playingGame() {
        System.out.println("Tong so cau hoi: " + questionList.size());
        System.out.println("Nhap so cau hoi trong game: ");
        int n = Integer.parseInt(scan.nextLine());
        if(n > questionList.size()) {
            n = questionList.size();
        }
        
        List<Question> tempList = new ArrayList<>();
        for (Question q : questionList) {
            tempList.add(q);
        }
        
        List<Question> games = new ArrayList<>();
        
        Random ran = new Random();
        for (int i = 0; i < n; i++) {
            int index = ran.nextInt(tempList.size());
            games.add(tempList.remove(index));
        }
        
        System.out.println("=== Pay Game ===");
        int correct  = 0;
        for (Question game : games) {
            boolean isCorrect = game.playing();
            if(isCorrect) correct++;
        }
        
        System.out.println("Ket qua: " + correct + "/" + n);
    }
    
    static void showMenu() {
        System.out.println("1. Nhap cau hoi");
        System.out.println("2. Tham gia game");
        System.out.println("3. Thoat");
        System.out.println("Chon: ");
    }
}
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 đó