By GokiSoft.com|
19:18 20/03/2023|
Java Advanced
[Source Code] Java Basic - Phần mềm AI LÀ TRIỆU PHÚ - C2206L
Java Basic - Phần mềm AI LÀ TRIỆU PHÚ
#Main.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.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 diepvan
*/
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:
input();
break;
case 2:
playGaming();
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 cau hoi");
System.out.println("2. Play game");
System.out.println("3. Thoat");
System.out.println("Chon: ");
}
private static void input() {
System.out.println("Nhap so cau hoi can them: ");
int N = Integer.parseInt(scan.nextLine());
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("questions.dat", true);
oos = new ObjectOutputStream(fos);
for (int i = 0; i < N; i++) {
Question question = new Question();
question.input();
oos.writeObject(question);
}
} 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);
}
}
}
}
private static void playGaming() {
List<Question> dataList = new ArrayList<>();
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("questions.dat");
ois = new ObjectInputStream(fis);
Object obj;
try {
while((obj = ois.readObject()) != null) {
Question q = (Question) obj;
dataList.add(q);
}
} catch(Exception e) {}
//Tiep tuc code logic -> OK
} 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);
}
}
}
//Doc xong du lieu
//Lay ngau nhien 15 cau hoi trong phan tren
List<Question> questionList = new ArrayList<>();
if(dataList.size() > 15) {
Random random = new Random();
int rad;
for (int i = 0; i < 15; i++) {
//dataList: A1, A2, A3, A4, A5
rad = random.nextInt(dataList.size());//rad: 0 - 5 -> 1 -> A2 trong mang dataList
Question q = dataList.remove(rad);
questionList.add(q);
}
} else {
questionList = dataList;
}
System.out.println("===== PLAY GAME =====");
int answerCorrect = 0;
for (Question question : questionList) {
boolean answer = question.gaming();
if(answer) answerCorrect++;
}
System.out.println("Ket qua: " + answerCorrect + "/" + questionList.size());
}
}
#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 com.gokisoft.java2.bt1376;
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> dataList;
int correctIndex = 0;
public Question() {
dataList = new ArrayList<>();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<String> getDataList() {
return dataList;
}
public void setDataList(List<String> dataList) {
this.dataList = dataList;
}
public int getCorrectIndex() {
return correctIndex;
}
public void setCorrectIndex(int correctIndex) {
this.correctIndex = correctIndex;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap cau hoi: ");
title = scan.nextLine();
System.out.println("Nhap dap an: ");
String line;
do {
System.out.println("Cau tra loi: ");
line = scan.nextLine();
if(!line.equalsIgnoreCase("N")) {
dataList.add(line);
}
} while(!line.equalsIgnoreCase("N"));
System.out.println("Cau tra loi chinh xac: ");
correctIndex = Integer.parseInt(scan.nextLine());
}
public boolean gaming() {
Scanner scan = new Scanner(System.in);
System.out.println("Cau hoi: " + title);
for (int i = 0; i < dataList.size(); i++) {
System.out.println("Cau " + (i+1) + ": " + dataList.get(i));
}
System.out.println("Dap an: ");
int index = Integer.parseInt(scan.nextLine());
return index == correctIndex;
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)