java 6. 编写一个四则运算测试程序,随机产生10道四则运算题目,并将题目在屏幕上显示,等待用户回答,统计得分,并输出最后得分,例如:“您做了10道题,其中正确X道,错误X道,最后得分:XX”。
时间: 2023-04-12 21:01:26 浏览: 82
好的,我可以回答这个问题。以下是一个 Java 6 的四则运算测试程序的代码:
import java.util.Random;
import java.util.Scanner;
public class ArithmeticTest {
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int correctCount = 0;
int wrongCount = 0;
for (int i = 1; i <= 10; i++) {
int num1 = random.nextInt(100);
int num2 = random.nextInt(100);
int operator = random.nextInt(4);
String operatorStr;
int result;
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;
default:
operatorStr = "";
result = 0;
}
System.out.print("Question " + i + ": " + num1 + " " + operatorStr + " " + num2 + " = ");
int answer = scanner.nextInt();
if (answer == result) {
System.out.println("Correct!");
correctCount++;
} else {
System.out.println("Wrong! The correct answer is " + result);
wrongCount++;
}
}
System.out.println("You answered " + correctCount + " questions correctly and " + wrongCount + " questions incorrectly.");
System.out.println("Your final score is " + (correctCount * 10) + ".");
}
}
希望这个程序能够帮助你进行四则运算的测试。
阅读全文