9月23

今天完成了课后第一题:
随机生成三十道四则运算题

import java.util.Random;

public class ArithmeticQuizGenerator {

private static final char[] OPERATORS = {'+', '-', '*', '/'};

public static void main(String[] args) {

    Random random = new Random();
    
    
    for (int i = 0; i < 30; i++) {
        int num1 = random.nextInt(100) + 1; 
        int num2 = random.nextInt(100) + 1; 
        char operator = OPERATORS[random.nextInt(OPERATORS.length)]; 
        
      
        if (operator == '/' && num2 == 0) {
            num2 = 1; 
        }
        
      
        System.out.println(num1 + " " + operator + " " + num2 + " = ?");
    }
}

}

posted @ 2024-09-23 23:06  杨家兴  阅读(6)  评论(0编辑  收藏  举报