在原有代码的基础上添加了年级分类
package guv;
import java.util.Random;
import java.util.Scanner;
import java.time.Duration;
import java.time.Instant;
class ArithmeticGenerator {
protected Random random;
protected Scanner scanner;
protected int correctCount;
protected int wrongCount;
protected int n;
protected int max = 30;
protected int totalTimeLimitSeconds = 60;
protected Instant startTime;
protected Instant endTime;
public ArithmeticGenerator() {
random = new Random();
scanner = new Scanner(System.in);
correctCount = 0;
wrongCount = 0;
startTime = Instant.now();
endTime = startTime.plusSeconds(totalTimeLimitSeconds);
}
public void generateAndCheckAnswer() {
// 需要在子类中实现具体的生成题目和检查答案逻辑
}
public void displayResult() {
double totalQuestions = 30;
double correctRate = correctCount / totalQuestions * 100;
System.out.println("你答对了 " + correctCount + " 道题。");
System.out.println("你答错了 " + wrongCount + " 道题。");
System.out.println("正确率为:" + correctRate + "%");
}
}
class SecondGradeArithmeticGenerator extends ArithmeticGenerator {
public void generateAndCheckAnswer() {
for (int i = 0; i < max; i++) {
int num1, num2, result;
String operator;
num1 = random.nextInt(100) + 1;
num2 = random.nextInt(100) + 1;
int operatorIndex = random.nextInt(4);
switch (operatorIndex) {
case 0:
operator = "+";
result = num1 + num2;
break;
case 1:
num1 = random.nextInt(100) + 1;
num2 = random.nextInt(num1) + 1;
operator = "-";
if ((num1 - num2) < 0) {
i--;
continue;
}
result = num1 - num2;
break;
case 2:
num1 = random.nextInt(100) + 1;
num2 = random.nextInt(100) + 1;
operator = "*";
result = num1 * num2;
break;
default:
operator = "/";
if (num2 == 0 || num1 % num2!= 0) {
i--;
continue;
}
result = num1 / num2;
break;
}
System.out.println(num1 + " " + operator + " " + num2 + " = ");
int userAnswer;
try {
userAnswer = scanner.nextInt();
} catch (Exception e) {
userAnswer = -1;
}
if (userAnswer == result) {
correctCount++;
} else {
wrongCount++;
}
Instant currentTime = Instant.now();
if (currentTime.isAfter(endTime)) {
break;
}
}
}
}
class ThirdGradeArithmeticGenerator extends SecondGradeArithmeticGenerator {
public void generateAndCheckAnswer() {
for (int i = 0; i < max; i++) {
int num1, num2, num3, num4;
int result;
String operator;
num1 = random.nextInt(1000) + 1;
num2 = random.nextInt(1000) + 1;
num3 = random.nextInt(1000) + 1;
num4 = random.nextInt(1000) + 1;
int operatorIndex = random.nextInt(4);
switch (operatorIndex) {
case 0:
operator = "+";
result = num1 + num2 + num3 + num4;
break;
case 1:
operator = "-";
result = num1 - num2 - num3 - num4;
if (result < 0) {
i--;
continue;
}
break;
case 2:
operator = "*";
result = num1 * num2 * num3 * num4;
break;
default:
operator = "/";
if (num2 == 0 || num3 == 0 || num4 == 0 || num1 % num2 % num3 % num4!= 0) {
i--;
continue;
}
result = num1 / num2 / num3 / num4;
break;
}
System.out.println(num1 + " " + operator + " " + num2 + " " + operator + " " + num3 + " " + operator + " " + num4 + " = ");
int userAnswer;
try {
userAnswer = scanner.nextInt();
} catch (Exception e) {
userAnswer = -1;
}
if (userAnswer == result) {
correctCount++;
} else {
wrongCount++;
}
Instant currentTime = Instant.now();
if (currentTime.isAfter(endTime)) {
break;
}
}
}
}
class FourthGradeArithmeticGenerator extends ThirdGradeArithmeticGenerator {
public void generateAndCheckAnswer() {
for (int i = 0; i < max; i++) {
int num1, num2, num3, num4, num5 = 0;
int result;
String operator;
num1 = random.nextInt(1000) + 1;
num2 = random.nextInt(1000) + 1;
num3 = random.nextInt(1000) + 1;
num4 = random.nextInt(1000) + 1;
int operatorIndex = random.nextInt(4);
switch (operatorIndex) {
case 0:
operator = "+";
result = num1 + num2 + num3 + num4 + num5;
break;
case 1:
operator = "-";
result = num1 - num2 - num3 - num4 - num5;
if (result < 0) {
i--;
continue;
}
break;
case 2:
operator = "*";
result = num1 * num2 * num3 * num4 * num5;
break;
default:
operator = "/";
if (num2 == 0 || num3 == 0 || num4 == 0 || num5 ==0 || num1 % num2 % num3 % num4 % num5!= 0) {
i--;
continue;
}
result = num1 / num2 / num3 / num4 /num5;
break;
}
System.out.println(num1 + " " + operator + " " + num2 + " " + operator + " " + num3 + " " + operator + " " + num4 +
" " +operator + " " + num5+" = ");
int userAnswer;
try {
userAnswer = scanner.nextInt();
} catch (Exception e) {
userAnswer = -1;
}
if (userAnswer == result) {
correctCount++;
} else {
wrongCount++;
}
Instant currentTime = Instant.now();
if (currentTime.isAfter(endTime)) {
break;
}
}
}
}
public class RandomArithmeticGenerator {
public static void main(String[] args) {
System.out.println("请选择年级(1 表示一年级,2 表示二年级,3 表示三年级):");
Scanner scanner = new Scanner(System.in);
int grade = scanner.nextInt();
ArithmeticGenerator generator;
if (grade == 2) {
generator = new SecondGradeArithmeticGenerator();
} else if (grade == 3) {
generator = new ThirdGradeArithmeticGenerator();
} else {
generator = new ArithmeticGenerator();
}
generator.generateAndCheckAnswer();
generator.displayResult();
scanner.close();
}
}
标签:10,14,num2,int,nextInt,result,operator,num1 From: https://www.cnblogs.com/jackson12138/p/18466039