java 编写一个四则运算测试程序,随机产生10道四则运算题目,并将题目在屏幕上显示,等待用户回答,统计得分,并输出最后得分,例如:“您做了10道题,其中正确X道,错误X道,最后得分:XX”。
时间: 2023-10-06 17:09:10 浏览: 25
下面是一个简单的Java代码实现:
```
import java.util.Random;
import java.util.Scanner;
public class ArithmeticQuiz {
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int correctCount = 0; // 记录正确答案的数量
int totalCount = 10; // 题目总数
for (int i = 1; i <= totalCount; i++) {
int num1 = random.nextInt(100);
int num2 = random.nextInt(100);
int operator = random.nextInt(4); // 0: +, 1: -, 2: *, 3: /
System.out.print("(" + i + ") ");
switch (operator) {
case 0:
System.out.print(num1 + " + " + num2 + " = ");
if (scanner.nextInt() == (num1 + num2)) {
correctCount++;
}
break;
case 1:
System.out.print(num1 + " - " + num2 + " = ");
if (scanner.nextInt() == (num1 - num2)) {
correctCount++;
}
break;
case 2:
System.out.print(num1 + " * " + num2 + " = ");
if (scanner.nextInt() == (num1 * num2)) {
correctCount++;
}
break;
case 3:
System.out.print(num1 + " / " + num2 + " = ");
if (scanner.nextInt() == (num1 / num2)) {
correctCount++;
}
break;
}
}
scanner.close();
int wrongCount = totalCount - correctCount; // 计算错误答案的数量
int score = correctCount * 10; // 计算得分
System.out.println("您做了" + totalCount + "道题,其中正确" + correctCount + "道,错误" + wrongCount + "道,最后得分:" + score);
}
}
```
该程序使用了Java内置的Random类来随机生成数字和运算符,使用了Scanner类来读取用户输入。在程序执行时,会依次产生10道四则运算题目,用户输入答案后会统计正确和错误的数量,最终计算得分并输出。