用Java写一个随机生成四则运算的代码
时间: 2023-04-09 21:02:26 浏览: 127
好的,我可以回答这个问题。以下是一个简单的Java代码,可以随机生成四则运算:
import java.util.Random;
public class FourOperations {
public static void main(String[] args) {
Random random = new Random();
int num1 = random.nextInt(100);
int num2 = random.nextInt(100);
int operator = random.nextInt(4);
String op = "";
int result = 0;
switch (operator) {
case 0:
op = "+";
result = num1 + num2;
break;
case 1:
op = "-";
result = num1 - num2;
break;
case 2:
op = "*";
result = num1 * num2;
break;
case 3:
op = "/";
result = num1 / num2;
break;
}
System.out.println(num1 + " " + op + " " + num2 + " = " + result);
}
}
希望这个代码对你有帮助!
阅读全文