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

Math Tutor程序:统计答题数据并传递至Student类的实现难题

Math Tutor答题统计功能修复方案

问题概述

开发Math Tutor数学辅导程序,基础功能已实现,但无法统计答题尝试次数、正确题数并传递至Student类完成得分计算与结果展示。当前在addition、subtraction、multiplication函数中统计数据时,无法将结果传递到主类并更新Student对象的属性。

现有代码问题分析

  1. 运算函数为static类型,无法直接操作主方法中的Student实例,也没有返回统计数据的渠道
  2. 每个运算函数重复创建Scanner实例,易引发输入读取异常
  3. Student类的方法命名不规范(如attempted(int)应为setAttempted(int)),且仅支持单次赋值,无法累计答题次数和正确数
  4. Student类的results()方法中得分计算存在整数除法精度问题

修改后的完整代码

Math_Tutor类

import java.util.Scanner;

public class Math_Tutor 
{
    public static void main(String[] args) 
    {
        int choice;
        Scanner input = new Scanner(System.in);
        
        System.out.print("请输入你的姓名: ");
        String name = input.nextLine();
        Student student = new Student();
        student.setName(name);
        
        do
        {
            System.out.println("*** 数学辅导菜单 ***");
            System.out.println(
                "选择题型:\n" +
                "1. 加法\n" +
                "2. 减法\n" +
                "3. 乘法\n" +
                "4. 退出"
            );
            
            System.out.print("输入你的选择: ");
            choice = input.nextInt();
            
            switch (choice)
            {
                case 1: 
                    addition(student, input);
                    break;
                case 2:
                    subtraction(student, input);
                    break;
                case 3:
                    multiplication(student, input);
                    break;
            }
            
        } while (choice != 4);
        
        student.results();
        input.close();
    }
    
    public static void addition(Student student, Scanner input)
    {
        int keepGoing;
        
        do 
        {
            int firstNum = (int)(Math.random() * 10);
            int secondNum = (int)(Math.random() * 10);
            int add = firstNum + secondNum;
            
            System.out.println(firstNum + " + " + secondNum + " ?");
            int addInput = input.nextInt();

            student.incrementAttempted(); // 累计尝试次数
            if (addInput == add) {
                System.out.println("正确!");
                student.incrementCorrect(); // 累计正确数
            } else {
                System.out.println("答案错误,正确答案是 " + add);
            }

            System.out.println("是否继续练习?\n"
                    + "1. 是\n"
                    + "2. 否");
            System.out.print("输入你的选择: ");
            keepGoing = input.nextInt();
            
        } while (keepGoing != 2);
    }
    
    public static void subtraction(Student student, Scanner input)
    {
        int keepGoing;

        do 
        {
            int firstNum = (int)(Math.random() * 10);
            int secondNum = (int)(Math.random() * 10);
            if (firstNum < secondNum)
            {
                int temp = firstNum;
                firstNum = secondNum;
                secondNum = temp;
            }
            int subtract = firstNum - secondNum;
            System.out.println(firstNum + " - " + secondNum + " ?");
            int subInput = input.nextInt();

            student.incrementAttempted();
            if (subInput == subtract) {
                System.out.println("正确!");
                student.incrementCorrect();
            } else {
                System.out.println("答案错误,正确答案是 " + subtract);
            }

            System.out.println("是否继续练习?\n"
                    + "1. 是\n"
                    + "2. 否");
            System.out.print("输入你的选择: ");
            keepGoing = input.nextInt();
       
        } while(keepGoing != 2);
    }
    
    public static void multiplication(Student student, Scanner input)
    {
        int keepGoing;

        do 
        {
            int firstNum = (int)(Math.random() * 10);
            int secondNum = (int)(Math.random() * 10);
            int multiply = firstNum * secondNum;
            System.out.println(firstNum + " * " + secondNum + " ?");
            int multInput = input.nextInt();

            student.incrementAttempted();
            if (multInput == multiply) {
                System.out.println("正确!");
                student.incrementCorrect();
            } else {
                System.out.println("答案错误,正确答案是 " + multiply);
            }

            System.out.println("是否继续练习?\n"
                    + "1. 是\n"
                    + "2. 否");
            System.out.print("输入你的选择: ");
            keepGoing = input.nextInt();
       
        } while(keepGoing != 2);
    }
}

Student类

public class Student 
{   
    private String studentName;
    private int attempted;
    private int correct;
    
    public void setName(String name)
    {
        studentName = name;
    }
    
    // 累计尝试次数
    public void incrementAttempted()
    {
        attempted++;
    }
    
    // 累计正确数
    public void incrementCorrect()
    {
        correct++;
    }
    
    public void results()
    {
        System.out.println("\n*** " + studentName + " 的练习结果 ***");
        System.out.println("总答题次数: " + attempted);
        System.out.println("正确答题数: " + correct);
        
        if (attempted == 0) {
            System.out.println("未进行任何答题");
            return;
        }
        
        // 计算得分,避免整数除法精度问题
        double score = ((double)correct / attempted) * 100;
        System.out.printf("%s 的得分: %.1f分\n", studentName, score);
        
        if (score >= 90)
            System.out.println("很棒,继续保持!");
        else if (score >= 80)
            System.out.println("做得不错!");
        else if (score >= 70)
            System.out.println("表现还行,继续努力!");
        else 
            System.out.println("需要多加练习哦!");
    }
}

关键修改说明

  1. 运算函数调整:

    • 将Student对象和主方法的Scanner传入运算函数,实现数据累计
    • 每次答题后调用Student的incrementAttempted()和incrementCorrect()方法更新统计数据
  2. Scanner优化:

    • 仅在主方法中创建一个Scanner实例,避免重复创建导致的输入读取异常
  3. Student类优化:

    • 修正属性命名,提升可读性
    • 添加累计统计方法,支持逐步更新答题数据
    • 修复得分计算的整数除法问题,强制转换为浮点型后计算
    • 完善结果输出逻辑,处理未答题的边界情况

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 10:35:08