分不同年级写题记分问题

需求分析:
1.分不同的年级;1年练习10以内的加减法;2年级练习20以内的
加减法;3年级练习10以内的乘除,100内的加减;4年级练习20
以内的乘除,1000内的加减;5年级小数、分数加减;6年级小数、
分数加减乘除。.
2.记分数;每次练习题量不一样,1-2年级10题,3-6年级20题,每
次做完可以统计做对的数量,给出分数;(最好能把题目和用户的输
入最后一块显示到屏幕上)。
3.题目采用随机数生成,操作符也是随机生成。

import java.util.Random;
import java.util.Scanner;

public class MathPractice {
    public static void main(String[] args) {
        // 随机数生成器
        Random random = new Random();  
        // 键盘输入
        Scanner scanner = new Scanner(System.in);
        
        // 按年级生成题目    
        int year; 
        System.out.print("请输入年级:");
        year = scanner.nextInt();
        
        // 根据年级生成题目数量
        int count;
        if (year <= 2) count = 10;
        else count = 20;
        
        // 统计做对的题目数量
        int rightCount = 0;
        
        // 显示每道题和用户输入
        String showProcess = ""; 
        
        // 循环生成题目
        for (int i = 0; i < count; i++) {
            int n1, n2, result;  // 两个操作数和结果
            
            // 根据年级生成操作符和操作数范围
            char opt;  
            if (year <= 2) {
                n1 = random.nextInt(10);
                n2 = random.nextInt(10);
                opt = '+';
            } else if (year <= 4) {
                n1 = random.nextInt(20);
                n2 = random.nextInt(20);
                opt = random.nextInt(2) == 0 ? '+' : '-';
            } else {
                // ... 其他年级题目
            }   
            
            // 计算结果
            switch(opt) {
                case '+': result = n1 + n2; break;
                case '-': result = n1 - n2; break;
                // ... 其他运算
            }
            
            // 显示题目和接收输入
            System.out.print(n1 + " " + opt + " " + n2 + " = ");
            int input = scanner.nextInt();
            
            // 判断并统计
            if (input == result) rightCount++;
            
            // 拼接显示内容
            showProcess += n1 + " " + opt + " " + n2 + " = " + input + "\n";
        }  
        
        // 显示做对数量和分数,以及所有题目和输入情况
        System.out.println("你做对了" + rightCount + "道题,得分为" + rightCount * 10 + "分");
        System.out.println(showProcess);
    }
}
import java.util.Scanner;
import java.util.Random;

public class MathPractice {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    Random rand = new Random();

    int level;
    int numQuestions;

    // 获取年级
    do {
      System.out.print("请输入您的年级(1-6):");
      level = input.nextInt();
    } while (level < 1 || level > 6);

    // 确定题目数量
    if (level <= 2) {
      numQuestions = 10;
    } else {
      numQuestions = 20;
    }

    // 记录得分
    int numCorrect = 0;

    // 生成题目
    for (int i = 0; i < numQuestions; i++) {
      int operand1;
      int operand2;
      int answer;
      char operator;

      // 根据年级生成题目
      switch (level) {
        case 1:
          operand1 = rand.nextInt(10);
          operand2 = rand.nextInt(10);
          operator = (rand.nextInt(2) == 0) ? '+' : '-';
          break;
        case 2:
          operand1 = rand.nextInt(20);
          operand2 = rand.nextInt(20);
          operator = (rand.nextInt(2) == 0) ? '+' : '-';
          break;
        case 3:
          operand1 = rand.nextInt(10);
          operand2 = rand.nextInt(11);
          operator = (rand.nextInt(2) == 0) ? '*' : '/';
          break;
        case 4:
          operand1 = rand.nextInt(20);
          operand2 = rand.nextInt(21);
          operator = (rand.nextInt(2) == 0) ? '*' : '/';
          break;
        case 5:
          operand1 = rand.nextInt(100);
          operand2 = rand.nextInt(100);
          operator = (rand.nextInt(2) == 0) ? '+' : '-';
          break;
        case 6:
          operand1 = rand.nextInt(100);
          operand2 = rand.nextInt(100);
          operator = (rand.nextInt(2) == 0) ? '+' : '-';
          if (operand2 == 0) {
            operator = '*';
          } else {
            // 确保结果不是小数
            while (operand1 % operand2 != 0) {
              operand1 = rand.nextInt(100);
              operand2 = rand.nextInt(100);
            }
          }
          break;
        default:
          operand1 = 0;
          operand2 = 0;
          operator = '+';
      }

      // 输出题目并获取答案
      System.out.print((i+1) + ". ");
      System.out.print(operand1 + " " + operator + " " + operand2 + " = ");
      answer = input.nextInt();

      // 检查答案
      switch (operator) {
        case '+':
          if (operand1 + operand2 == answer) {
            System.out.println("正确!");
            numCorrect++;
          } else {
            System.out.println("错误!");
          }
          break;
        case '-':
          if (operand1 - operand2 == answer) {
            System.out.println("正确!");
            numCorrect++;
          } else {
            System.out.println("错误!");
          }
          break;
        case '*':
          if (operand1 * operand2 == answer) {
            System.out.println("正确!");
            numCorrect++;
          } else {
            System.out.println("错误!");
          }
          break;
        case '/':
          if (operand1 / operand2 == answer) {
            System.out.println("正确!");
            numCorrect++;
          } else {
            System.out.println("错误!");
          }
          break;
      }
    }

    // 输出分数
    int score = (int) ((double) numCorrect / numQuestions * 100);
    System.out.println("您的分数为:" + score);
  }
}