作业

第一个:
这段代码展示了 ** 方法重载(Method Overloading)** 的特性。
方法重载是指在同一个类中,可以定义多个同名的方法,只要它们的参数列表(参数的类型、个数、顺序)不同即可。
在这段代码里:
有两个名为 square 的方法,一个接收 int 类型的参数,返回 int 类型的平方值;
另一个接收 double 类型的参数,返回 double 类型的平方值。
在 main 方法中,调用 square 方法时,会根据传入参数的类型(int 型的 7 或 double 型的 7.5),自动匹配对应的方法进行调用,从而实现了对不同数据类型的数进行平方运算的功能。
第二个:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;

public class sizeyunsuan {
static class WrongQuestion {
String question;
int correctAnswer;

    WrongQuestion(String question, int correctAnswer) {
        this.question = question;
        this.correctAnswer = correctAnswer;
    }
}
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Random random = new Random();
    String[] operators = {"+", "-", "*", "/"};
    int totalCount=30;
    int correctCount=0;
    List<WrongQuestion> wrongQuestions=new ArrayList<>();
    Set<String> generatedQuestions = new HashSet<>();

    correctCount = getCorrectCount(random, operators, sc, correctCount, wrongQuestions);

    int wrongCount = getWrongCount(totalCount, correctCount);

    extracted(wrongCount, wrongQuestions);

    sc.close();
}

private static int getWrongCount(int totalCount, int correctCount) {
    int wrongCount = totalCount - correctCount;
    double accuracy = (double) correctCount / totalCount * 100;

    System.out.println("\n答题结束!");
    System.out.println("总题数:" + totalCount);
    System.out.println("做对:" + correctCount + "题");
    System.out.println("做错:" + wrongCount + "题");
    System.out.printf("正确率:%.2f%%\n", accuracy);
    return wrongCount;
}

private static void extracted(int wrongCount, List<WrongQuestion> wrongQuestions) {
    if (wrongCount > 0) {
        System.out.println("\n错题回顾:");
        for (int i = 0; i < wrongQuestions.size(); i++) {
            WrongQuestion q = wrongQuestions.get(i);
            System.out.printf("%d. %s %d\n",
                    (i + 1), q.question, q.correctAnswer);
        }
    }
}

//生成三十个题目
private static int getCorrectCount(Random random, String[] operators, Scanner sc, int correctCount, List<WrongQuestion> wrongQuestions) {
    for (int i = 0; i < 30; i++) {
        int num1 = random.nextInt(20) + 1; // 1-20的随机数
        int num2 = random.nextInt(20) + 1;
        String operator = operators[random.nextInt(4)]; // 随机选择运算符

        if (operator.equals("-") && num2 > num1) {
            int temp = num1;
            num1 = num2;
            num2 = temp;
        }

        if (operator.equals("/")) {
            while (num2 == 0 || num1 % num2 != 0) {
                num1 = random.nextInt(20) + 1;
                num2 = random.nextInt(20) + 1;
            }
        }

        int correctAnswer = calculate(num1, num2, operator);

        System.out.printf("第%d题:%d %s %d = ", (i + 1), num1, operator, num2);
        int userAnswer = sc.nextInt();

        if (userAnswer == correctAnswer) {
            correctCount++;
            System.out.println("回答正确!");
        } else {
            wrongQuestions.add(new WrongQuestion(
                    num1 + " " + operator + " " + num2 + " = ",
                    correctAnswer
            ));
            System.out.println("回答错误,正确答案是:" + correctAnswer);
        }
    }
    return correctCount;
}
private static int calculate(int num1, int num2, String operator) {
    switch (operator) {
        case "+":
            return num1 + num2;
        case "-":
            return num1 - num2;
        case "*":
            return num1 * num2;
        case "/":
            return num1 / num2;
        default:
            return 0;
    }
}

}

posted @ 2025-10-08 12:09  把我当马嘉祺整  阅读(5)  评论(0)    收藏  举报