Java算术运算测试:功能要求:该程序实现十道100以内加减法数学 题,能根据题目计算出答案,与输入答案对比,判断做题是否正确, 最后计算分数
时间: 2023-05-28 11:02:34 浏览: 688
java测试专用源代码
。以下是Java算术运算测试的代码示例:
```java
import java.util.Random;
import java.util.Scanner;
public class ArithmeticTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int score = 0;
for (int i = 1; i <= 10; i++) {
int a = random.nextInt(100);
int b = random.nextInt(100);
int operator = random.nextInt(2);
int result;
if (operator == 0) {
result = a + b;
System.out.print(i + ". " + a + " + " + b + " = ");
} else {
result = a - b;
System.out.print(i + ". " + a + " - " + b + " = ");
}
int answer = sc.nextInt();
if (answer == result) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Wrong!");
}
}
System.out.println("Your score is " + score + " out of 10.");
}
}
```
该程序使用了Scanner和Random类,通过循环生成10道随机的加减法运算题,计算出正确答案并与用户输入的答案对比,正确则加1分,最后输出得分。通过随机生成题目和答案,确保每次程序运行的题目不同。
阅读全文