今天的课都是硬课啊!!!!
import java.util.Random;
public class RandomArithmetic {
public static void main(String[] args) {
Random random = new Random();
int numOperations = 30;
for (int i = 0; i < numOperations; i++) {
int num1 = random.nextInt(90) + 10;
int num2 = random.nextInt(90) + 10;
String operator = getRandomOperator();
double result = evaluate(num1, num2, operator);
System.out.printf("%d %s %d = %.2f%n", num1, operator, num2, result);
}
}
private static String getRandomOperator() {
Random random = new Random();
int operation = random.nextInt(4);
switch (operation) {
case 0: return "+";
case 1: return "-";
case 2: return "*";
case 3: return "/";
default: return "+";
}
}
private static double evaluate(int num1, int num2, String operator) {
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
if (num2 != 0) {
return (double) num1 / num2;
} else {
return 0;
}
default:
return 0;
}
}
}
标签:case,return,num1,num2,9.24,int,Random From: https://www.cnblogs.com/lala-la/p/18428068