创建杯子问题java基础

创建杯子 每种颜色创建一个杯子 杯子容量500 800 1000随机 将这些杯子存放在数组中


import java.util.Random;

class Cup {
    private String color;
    private int capacity;

    public Cup(String color, int capacity) {
        this.color = color;
        this.capacity = capacity;
    }

    public String getColor() {
        return color;
    }

    public int getCapacity() {
        return capacity;
    }
}

public class Main {
    public static void main(String[] args) {
        String[] colors = {"red", "green", "blue"};
        Random random = new Random();

        // Create an array of cups
        Cup[] cups = new Cup[colors.length];
        for (int i = 0; i < cups.length; i++) {
            cups[i] = new Cup(colors[i], random.nextInt(501) + 500);
        }

        // Print out the cups' details
        for (Cup cup : cups) {
            System.out.println("Color: " + cup.getColor() + ", Capacity: " + cup.getCapacity());
        }
    }
}