java小学生四则混合运算
时间: 2023-10-20 21:06:10 浏览: 80
好的,我可以给你一个生成随机四则混合运算的Java程序,你可以在控制台上进行输入和输出。以下是代码示例:
```java
import java.util.Random;
import java.util.Scanner;
public class MathQuiz {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random();
int num1, num2, operator, answer, userAnswer, count = 0;
while (count < 10) {
num1 = random.nextInt(101);
num2 = random.nextInt(101);
operator = random.nextInt(4);
switch (operator) {
case 0:
answer = num1 + num2;
System.out.print(num1 + " + " + num2 + " = ");
break;
case 1:
answer = num1 - num2;
System.out.print(num1 + " - " + num2 + " = ");
break;
case 2:
answer = num1 * num2;
System.out.print(num1 + " * " + num2 + " = ");
break;
case 3:
answer = num1 / num2;
System.out.print(num1 + " / " + num2 + " = ");
break;
default:
answer = 0;
break;
}
userAnswer = input.nextInt();
if (userAnswer == answer) {
System.out.println("Correct!");
} else {
System.out.println("Wrong. The answer should be " + answer);
}
count++;
}
}
}
```
这个程序会随机生成10个四则混合运算题目,每次用户输入答案后会给予正确或错误的反馈。你可以根据需要修改程序,比如增加题目数量或者修改题目的难度等。
阅读全文