用java编写一个不同年级自主学习的四则运算练习的程序,要求完成如下功能: (1) 用随机函数生成10个随机的四则运算,限制在两位数以内,超过的则用异常提醒 (2) 对于减法和除法要判断结果不能出现负数和除数不能为零,同时处理异常。 (3) 用户输入每一个的运算结果,然后并判断对错,对则加一分,最终计算出最终得分。 (4) 菜单能够循环让用户选择是否继续,直到选择结束而关闭程序。 (5) 判断对错需要一个单独的类,计分需要一个单独的类 (6) 一年级的学生,只有加减;三年级学生有加减乘除。构建父类和子类关系
时间: 2023-06-20 15:04:19 浏览: 106
以下是一个简单的实现,包含了一个父类和两个子类,分别为一年级和三年级的学生。其中,父类为题目生成器类,子类为题目校验类和计分类。
题目生成器类:
```java
import java.util.Random;
public class QuestionGenerator {
private Random random = new Random();
public String generateQuestion() throws Exception {
int num1 = random.nextInt(100);
int num2 = random.nextInt(100);
int operator = random.nextInt(4);
String op;
int result;
switch (operator) {
case 0:
op = "+";
result = num1 + num2;
break;
case 1:
op = "-";
if (num1 < num2) {
throw new Exception("Subtraction may produce negative result");
}
result = num1 - num2;
break;
case 2:
op = "*";
result = num1 * num2;
break;
case 3:
op = "/";
if (num2 == 0) {
throw new Exception("Division by zero");
}
if (num1 % num2 != 0) {
throw new Exception("Division may produce non-integer result");
}
result = num1 / num2;
break;
default:
throw new Exception("Invalid operator");
}
return String.format("%d %s %d = ", num1, op, num2);
}
}
```
题目校验类:
```java
public class AnswerValidator {
public static boolean isValid(int answer, String question) throws Exception {
String[] parts = question.split(" ");
int num1 = Integer.parseInt(parts[0]);
String op = parts[1];
int num2 = Integer.parseInt(parts[2]);
int expected;
switch (op) {
case "+":
expected = num1 + num2;
break;
case "-":
expected = num1 - num2;
break;
case "*":
expected = num1 * num2;
break;
case "/":
if (num2 == 0) {
throw new Exception("Division by zero");
}
if (num1 % num2 != 0) {
throw new Exception("Division may produce non-integer result");
}
expected = num1 / num2;
break;
default:
throw new Exception("Invalid operator");
}
return answer == expected;
}
}
```
计分类:
```java
public class ScoreCounter {
private int score = 0;
public void addScore() {
score++;
}
public int getScore() {
return score;
}
}
```
主程序:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
QuestionGenerator generator = new QuestionGenerator();
ScoreCounter counter = new ScoreCounter();
boolean keepGoing = true;
while (keepGoing) {
System.out.print("Please enter the grade level (1 or 3): ");
int grade = scanner.nextInt();
scanner.nextLine();
int numQuestions = 10;
if (grade == 1) {
System.out.println("Generating 10 addition/subtraction questions for first grade...");
} else if (grade == 3) {
System.out.println("Generating 10 addition/subtraction/multiplication/division questions for third grade...");
} else {
System.out.println("Invalid grade level");
continue;
}
for (int i = 1; i <= numQuestions; i++) {
String question;
try {
question = generator.generateQuestion();
} catch (Exception e) {
System.out.println(e.getMessage());
i--;
continue;
}
System.out.print(question);
int answer = scanner.nextInt();
scanner.nextLine();
boolean validAnswer;
try {
validAnswer = AnswerValidator.isValid(answer, question);
} catch (Exception e) {
System.out.println(e.getMessage());
i--;
continue;
}
if (validAnswer) {
System.out.println("Correct!");
counter.addScore();
} else {
System.out.println("Wrong!");
}
}
System.out.printf("Your score is %d out of %d\n", counter.getScore(), numQuestions);
System.out.print("Do you want to continue (y/n)? ");
String answer = scanner.nextLine();
keepGoing = answer.equalsIgnoreCase("y");
}
}
}
```
阅读全文