You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java题库重复题目规避与多输出问题技术咨询

问题解决:Java抽题程序的两个修复需求

需求1:实现不重复抽题并存储已使用题目到长度为10的数组ArrayB

核心思路

要实现不重复抽题,需要先记录已使用的题目索引,确保每次抽取的题目未被选中过;同时创建长度为10的数组,专门存储已抽取的题目内容。

具体实现

  1. 用ArrayList<Integer>记录已使用的题目索引,每次生成随机数后检查是否在列表中,若存在则重新生成,直到找到未使用的索引;
  2. 创建usedQuestions数组(即需求中的ArrayB),将每次选中的题目存入该数组对应位置。

需求2:修复程序输出11道题的问题

问题根源

  1. main方法中用input.next()读取用户信息后,Scanner缓冲区残留换行符,导致returning方法里第一次input.nextLine()读取到空字符串,程序直接跳过第一道题的输入流程,继续输出下一道题;
  2. returning方法的for循环开头错误执行correctCount++和incorrectCount++,不仅计数逻辑混乱,也间接引发流程异常。

修复方案

  1. 在main方法读取完用户信息后,添加input.nextLine()清空缓冲区的换行符;
  2. 删除for循环开头错误的计数递增语句,仅在判断答案对错时更新计数。

修改后的完整代码

import java.util.Scanner;
import java.util.ArrayList;

public class QuizProgram {
    public static void main(String[] args) {
        String firstName;
        String lastName;
        String major;
        String level;
        int correctCounter = 0;
        int incorrectCounter = 0;

        Scanner input = new Scanner(System.in);

        System.out.println("请输入以下信息:");
        System.out.print("名字: ");
        firstName = input.next();

        System.out.print("姓氏: ");
        lastName = input.next();

        System.out.print("你的专业: ");
        major = input.next();

        System.out.print("年级(大一/大二/大三/大四): ");
        level = input.next();
        // 清空缓冲区残留的换行符,避免影响后续输入读取
        input.nextLine();
        System.out.println();

        // 20道题的数组
        String[] questions = new String[20];
        questions[0] = "判断题:成本是团队协作的潜在劣势?";
        questions[1] = "判断题:能力不是影响沟通的文化核心要素。";
        questions[2] = "以下哪项不是会议常用技术?\n"
                + "A. 工作组消息工具\n"
                + "B. 手机\n"
                + "C. 电话会议\n"
                + "D. 视频会议";
        questions[3] = "倾听流程的第一步是什么?\n"
                + "A. 评估\n"
                + "B. 记忆\n"
                + "C. 回应\n"
                + "D. 接收\n"
                + "E. 解码";
        questions[4] = "______ 是指通过书面、口头、视觉或数字媒介,在发送者和接收者之间传递信息与意义的过程。";
        questions[5] = "______ 是社会中公认的行为准则,规范人们的行为。";
        questions[6] = "倾听流程包含几个步骤?";
        questions[7] = "iTranslate支持翻译超过 __ 种语言,且具备语音输入输出功能。";
        questions[8] = "1/4用小数表示是多少?";
        questions[9] = "成年人的正常体温是多少华氏度(°F)?";
        questions[10] = "风筝挂在树枝上,风筝线长90英尺,与地面夹角为22度,计算风筝到地面的距离(单位:英尺)。";
        questions[11] = "若y=cos x,y的最大值是多少?";
        questions[12] = "6的3次方是多少?";
        questions[13] = "3的6次方是多少?";
        questions[14] = "将672.4四舍五入到整数。";
        questions[15] = "将14.6四舍五入到整数。";
        questions[16] = "找出15、32、10、99、87中的最大值。";
        questions[17] = "找出15、32、10、99、87中的最小值。";
        questions[18] = "群体思维是指同伴压力导致团队成员隐瞒相反的______,被迫认同自己并不认可的决策。";
        questions[19] = "大多数人不想让他人失望,参与团队会让人产生对他人的______感。";

        // 接收返回的正确/错误计数
        int[] counts = returning(questions);
        correctCounter = counts[0];
        incorrectCounter = counts[1];

        // 生成测试报告
        report(firstName, lastName, major, level, correctCounter, incorrectCounter);

        input.close();
    }

    public static int[] returning(String[] questions) {
        Scanner input = new Scanner(System.in);

        // 答案数组,与题目索引一一对应
        String[] answers = new String[20];
        answers[0] = "True";
        answers[1] = "False";
        answers[2] = "B";
        answers[3] = "D";
        answers[4] = "Communication";
        answers[5] = "Ethics";
        answers[6] = "5";
        answers[7] = "80";
        answers[8] = "0.25";
        answers[9] = "98.6";
        answers[10] = "33.71";
        answers[11] = "1";
        answers[12] = "216";
        answers[13] = "729";
        answers[14] = "672";
        answers[15] = "15";
        answers[16] = "99";
        answers[17] = "10";
        answers[18] = "opinions";
        answers[19] = "accountability";

        int correctCount = 0;
        int incorrectCount = 0;
        // 记录已使用的题目索引,避免重复抽取
        ArrayList<Integer> usedIndices = new ArrayList<>();
        // 存储已抽取的题目(需求中的ArrayB)
        String[] usedQuestions = new String[10];

        for (int i = 0; i < 10; i++) {
            int randomNumber;
            // 生成未使用过的随机题目索引
            do {
                randomNumber = (int) (Math.random() * 20);
            } while (usedIndices.contains(randomNumber));

            // 标记该索引为已使用
            usedIndices.add(randomNumber);
            // 将题目存入已使用数组
            usedQuestions[i] = questions[randomNumber];

            // 输出题目并读取用户答案
            System.out.println(questions[randomNumber]);
            String userAnswer = input.nextLine().trim();

            // 判断答案是否正确
            if (userAnswer.equalsIgnoreCase(answers[randomNumber])) {
                correctCount++;
            } else {
                incorrectCount++;
            }
        }

        // 可选:打印已抽取的题目列表
        System.out.println("\n已抽取的题目:");
        for (String q : usedQuestions) {
            System.out.println("- " + q);
        }

        return new int[]{correctCount, incorrectCount};
    }

    public static void report(String firstName, String lastName, String major,
                              String level, int correctCount, int incorrectCount) {
        // 计算正确率,用10.0避免整数除法导致的精度丢失
        double percentCorrect = (correctCount / 10.0) * 100;

        System.out.println("\n=== 测试报告 ===");
        System.out.println("姓名: " + firstName + " " + lastName);
        System.out.println("专业: " + major);
        System.out.println("年级: " + level);
        System.out.println("答对题目数: " + correctCount);
        System.out.println("答错题目数: " + incorrectCount);
        System.out.printf("正确率: %.1f%%\n", percentCorrect);
    }
}

关键修改说明

  1. 不重复抽题与存储:

    • 通过ArrayList<Integer>跟踪已使用的题目索引,确保每道题只被抽取一次;
    • 新增usedQuestions数组存储已抽取的题目,满足需求中ArrayB的要求。
  2. 修复多输出题目问题:

    • 清理Scanner缓冲区的残留换行符,避免输入读取异常;
    • 修正计数逻辑,仅在答案判断正确或错误时更新计数;
    • 将分散的答案变量改为数组,简化代码逻辑;
    • 调整returning方法返回值为数组,同时返回正确和错误计数,解决原代码返回值无意义的问题。

内容的提问来源于stack exchange,提问作者sgraham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 21:50:23