java小学四则运算测试程序
时间: 2023-11-25 18:49:36 浏览: 115
基于Java-GUI的四则运算练习系统.rar
5星 · 资源好评率100%
以下是一个Java小学生四则运算测试程序的示例代码:
```java
import java.util.Random;
import java.util.Scanner;
public class MathTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random();
int score = 0; // 记录分数
int count = 0; // 记录题目数量
while (true) {
int num1 = random.nextInt(100); // 生成0-99之间的随机整数
int num2 = random.nextInt(100);
int operator = random.nextInt(4); // 生成0-3之间的随机整数,分别代表加减乘除
String operatorStr = "";
int result = 0; // 记录正确答案
switch (operator) {
case 0:
operatorStr = "+";
result = num1 + num2;
break;
case 1:
operatorStr = "-";
result = num1 - num2;
break;
case 2:
operatorStr = "*";
result = num1 * num2;
break;
case 3:
operatorStr = "/";
result = num1 / num2;
break;
}
System.out.print((count + 1) + ". " + num1 + " " + operatorStr + " " + num2 + " = ");
int answer = input.nextInt();
if (answer == result) {
System.out.println("恭喜你,回答正确!");
score += 10;
} else {
System.out.println("很遗憾,回答错误。正确答案是:" + result);
}
count++;
if (count == 30) {
break;
}
}
System.out.println("测试结束,的得分是:" + score);
}
}
```
该程序会随机生成30道小学生四则运算题,用户需要输入答案,程序会判断答案是否正确,并记录分数。最后输出用户的得分。
阅读全文