Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从产品类创建随机对象_Java_Random - Fatal编程技术网

Java 从产品类创建随机对象

Java 从产品类创建随机对象,java,random,Java,Random,我有一个表示产品的产品类: import java.util.ArrayList; public class Product { private int productId; private String productType; private String brand; private double price; public Product(int productId, String productType, String brand, doub

我有一个表示产品的产品类:

import java.util.ArrayList;

public class Product {
    private int productId;
    private String productType;
    private String brand;
    private double price;

    public Product(int productId, String productType, String brand, double price)
    {
        this.productId = productId;
        this.productType = productType;
        this.brand = brand;
        this.price = price;
    }

    public int getProductId() {
        return this.productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductType() {
        return this.productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public String getBrand() {
        return this.brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
现在,在我的程序中,我想创建这个产品类的3个随机对象,下面是我的代码:

public static void main(String[] args) {
    ArrayList<Product> products = new ArrayList();
    Random r = new Random();

    for(int i = 0; i < 3; i++)
    {
        products.add(new Product(1337, "Type", "Brand", 300.33));
    }
}
publicstaticvoidmain(字符串[]args){
ArrayList产品=新的ArrayList();
随机r=新随机();
对于(int i=0;i<3;i++)
{
添加(新产品(1337,“类型”,“品牌”,300.33));
}
}

现在,我的问题是如何实现随机类创建随机值?我已经为产品创建了静态值,那么如何将其随机化以获得3个不同的值?

如果不知道要随机化产品的
元素,人们只能不断猜测。下面给出的代码不是您问题的解决方案;而是为您提供一个指针,说明如何使用随机生成的值:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Product {
    private int productId;
    private String productType;
    private String brand;
    private double price;

    public Product(int productId, String productType, String brand, double price) {
        this.productId = productId;
        this.productType = productType;
        this.brand = brand;
        this.price = price;
    }

    public int getProductId() {
        return this.productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductType() {
        return this.productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public String getBrand() {
        return this.brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product [productId=" + productId + ", productType=" + productType + ", brand=" + brand + ", price="
                + price + "]";
    }

    public static void main(String[] args) {
        List<Product> products = new ArrayList<>();
        Random r = new Random(10);
        int num;
        for (int i = 0; i < 3; i++) {
            num = r.nextInt(10);
            products.add(new Product(r.nextInt(10000), "Type" + num, "Brand" + num, 1000 * r.nextDouble()));
        }
        products.stream().forEach(System.out::println);
    }
}

如果您需要任何进一步的帮助,请随时发表评论。

当“相关”代码没有使用Swing类时,这是一个很好的提示,表明标记与问题无关。所以不要加上它!您希望随机字符串(类型和品牌)是怎样的?还是一个随机id?我投了反对票,因为在谷歌搜索结果的最上面(“java生成随机字符串”),我找到了多篇文章,向您展示了如何做到这一点。您是否有一组固定的有效品牌、id、。。。或者“随机化”对你意味着什么?“我的任意古怪品牌”是否足够随意?甚至“khkghfjkbvnmdk”?附加数字的静态值是否足够随机?
Product [productId=2380, productType=Type3, brand=Brand3, price=257.8027905957804]
Product [productId=1456, productType=Type6, brand=Brand6, price=244.11725056425314]
Product [productId=6214, productType=Type1, brand=Brand1, price=370.6111260136414]