java 编写一个四则运算测试程序,随机产生10道四则运算题目,并将题目在屏幕上显示,等待用户回答,统计得分,并输出最后得分,例如:“您做了10道题,其中正确X道,错误X道,最后得分:XX”。
时间: 2023-04-12 15:00:28 浏览: 99
好的,我可以回答这个问题。以下是一个Java编写的四则运算测试程序的示例代码:
```java
import java.util.Random;
import java.util.Scanner;
public class ArithmeticTest {
public static void main(String[] args) {
int numQuestions = 10;
int numCorrect = 0;
int numWrong = 0;
Random random = new Random();
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= numQuestions; i++) {
int a = random.nextInt(100);
int b = random.nextInt(100);
int op = random.nextInt(4);
int result;
String operator;
switch (op) {
case 0:
result = a + b;
operator = "+";
break;
case 1:
result = a - b;
operator = "-";
break;
case 2:
result = a * b;
operator = "*";
break;
case 3:
result = a / b;
operator = "/";
break;
default:
throw new IllegalStateException("Unexpected value: " + op);
}
System.out.print(i + ". " + a + " " + operator + " " + b + " = ");
int answer = scanner.nextInt();
if (answer == result) {
System.out.println("Correct!");
numCorrect++;
} else {
System.out.println("Wrong! The answer is " + result);
numWrong++;
}
}
System.out.println("You answered " + numCorrect + " questions correctly and " + numWrong + " questions incorrectly.");
System.out.println("Your final score is " + (numCorrect * 10) + ".");
}
}
```
这个程序会随机生成10道四则运算题目,并等待用户输入答案。用户回答后,程序会判断答案是否正确,并统计得分。最后,程序会输出用户的得分。
阅读全文