期末考试选择题刷题程序,简陋版,有兴趣可以优化23333

为的加强记忆,背答案,考试的时候好直接输出

效果:

期末考试选择题刷题程序,简陋版,有兴趣可以优化23333_第1张图片

期末考试选择题刷题程序,简陋版,有兴趣可以优化23333_第2张图片
思路:
java文件读取,加字符串分割,分割的花样很多,可以根据自己的喜好分割。

我的格式是:

问题1?A…B…C…D…?B#
问题2?A…B…C…D…?B#
问题3?A…B…C…D…?B#

先对#分割,分出每个题,之后对每个题的?进行分割,分出问题、选项和答案

上代码:

package com.study;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author wzhongy
 * @Description:
 * @Date 2022-12-12 10:31
 */
public class reading {
	public static void main(String[] args) throws IOException {
		ArrayList<String> al = new ArrayList<>();

		String s = ""; // 获取文件中的所有字符
		int cnt = 0; // 记录个数

		String filePath = "E:\\en\\reading.txt";
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(filePath);
			byte[] bytes = new byte[4];//每一次读取四个字节
			int readCount = 0;

			// 读取文件信息
			while ((readCount = fileInputStream.read(bytes)) != -1) {
				String c = new String(bytes,0,readCount);//将字节数组转换为字符串
				s += c;
			}


			// 将文件信息存入字符串数组,进行分割
			String[] sourceArray = s.split("#");
			boolean[] mk = new boolean[sourceArray.length];

//			for(int i = 0;i < mk.length;i ++) System.out.println(mk[i]);

			for(int i = 0;i < sourceArray.length-1;i ++){
				if(i%5 == 0) System.out.println("新的五道题:");
				String[] questArray = sourceArray[i].split("\\?");

				System.out.println(questArray[0]);
				System.out.println(questArray[1]);
				Scanner sc = new Scanner(System.in);
				String a = sc.next();

				if(a.equals(questArray[2])){
					System.out.println("Yes ! You are right !!");
					cnt ++;
				}else{
					System.out.println("No ! That is error !!");
					mk[i] = true;
					System.out.println("正确答案是:"+ questArray[2]);
				}

			}

			System.out.println("题目总个数: " + (sourceArray.length-1) + "正确个数:" + cnt);
			cnt = sourceArray.length-1-cnt;
			while(cnt != 0){
				System.out.println("");
				System.out.println("");
				System.out.println("恭喜你,奖励你做错的再来一遍!!");
				System.out.println("");
				System.out.println("");
				int t = 0;
				for(int i = 0;i < mk.length-1;i ++){
					if(mk[i]){
						String[] questArray = sourceArray[i].split("\\?");

						System.out.println(questArray[0]);
						System.out.println(questArray[1]);
						Scanner sc = new Scanner(System.in);
						String a = sc.next();

						if(a.equals(questArray[2])){
							System.out.println("Yes ! You are right !!");
							t ++;
							mk[i] = false;
						}else{
							System.out.println("No ! That is error !!");
							System.out.println("正确答案是:"+ questArray[2]);
						}
					}
				}

				System.out.println("题目总个数: " + cnt + "正确个数:" + t);
				cnt = cnt - t;
			}



			System.out.println("程序结束");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			fileInputStream.close();
		}
	}
}

你可能感兴趣的:(后端学习,java)