public String[] genRandomMathExpression() { Random random = new Random(); // 產生兩個 0 到 99…
- User
public String[] genRandomMathExpression() { Random random = new Random();
// 產生兩個 0 到 99 之間的隨機數 int num1 = random.nextInt(10); int num2 = random.nextInt(10); // 隨機選擇運算符號 char[] operators = {'+', '-', '*', '/'}; char operator = operators[random.nextInt(operators.length)]; // 隨機決定是否要加括號 boolean useParentheses = random.nextBoolean(); // 計算結果 double result; switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num2 != 0 ? (double) num1 / num2 : 0; break; // 避免除數為 0 default: result = 0; } // 格式化輸出 String expression = useParentheses ? "(" + num1 + " " + operator + " " + num2 + ")" : num1 + " " + operator + " " + num2; return new String[]{expression, String.valueOf(result)}; } Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.