如何为数组中的每个元素显示不同的消息?

时间:2018-11-02 15:16:12

标签: java

我应该为商店中的每种产品设置不同的类别,如果客户选择笔记本电脑,那么它将显示可用的笔记本电脑。如何创建由5台笔记本电脑组成的阵列,并为每台笔记本电脑显示不同的笔记本电脑? 这是我的测试仪

if (choice == "Laptop"){

    Laptop[] lap = new Laptop[5];

    for (int i = 0; i < lap.length; i++) {
        lap[i] = new Laptop();
    }
    lap[1].msg();

示例输出:

What do you want to buy?  Laptop

We have 5 different types of laptops:
1.  Manufacturer: HP, Price: $400, Hard drive: 100GB, RAM: 6 GB
2.  Manufacturer: HP, Price: $800, Hard drive: 200GB, RAM: 8 GB
3.  Manufacturer: Dell, Price: $300, Hard drive: 80GB, RAM: 6 GB
4.  Manufacturer: Dell, Price: $680, Hard drive: 150GB, RAM: 8 GB
5.  Manufacturer: Toshiba, Price: $350, Hard drive: 60GB, RAM: 4 GB

1 个答案:

答案 0 :(得分:1)

由于您在循环的每一遍上都创建了一个新的Laptop,因此每次都将获得相同的对象。除非您在Laptop类中设置字段,否则它们之间将看不到其他数据。

相反,您的Laptop类需要在构造函数中(创建每个Laptop时)或通过setter来设置其字段。

这是一个快速,简单的演示应用程序,您可以尝试并查看实际应用:

class LaptopDemo {
    public static void main(String[] args) {

        // First, create some sample laptops and add them to an Array
        Laptop[] laptops = new Laptop[5];

        // You need to create a new laptop and set it's field values
        laptops[0] = new Laptop("HP", 400.00, 100, 6);
        laptops[1] = new Laptop("HP", 800.00, 80, 8);
        laptops[2] = new Laptop("Dell", 300.00, 150, 6);
        laptops[3] = new Laptop("Dell", 680.00, 100, 8);
        laptops[4] = new Laptop("Toshiba", 350.00, 60, 4);

        // Now you can loop through the array and print the details
        for (int i = 0; i < laptops.length; i++) {

            // Build the String to be printed
            StringBuilder sb = new StringBuilder();
            sb.append("Manufacturer: ").append(laptops[i].getManufacturer()).append(", ")
                    .append("Price: $").append(laptops[i].getPrice()).append(", ")
                    .append("Hard Drive: ").append(laptops[i].getHdCapacity()).append("GB, ")
                    .append("RAM: ").append(laptops[i].getRamCapacity()).append("GB");

            System.out.println(sb.toString());
        }
    }
}

class Laptop {

    private final String manufacturer;
    private final double price;
    private final int hdCapacity;
    private final int ramCapacity;

    public Laptop(String manufacturer, double price, int hdCapacity, int ramCapacity) {
        this.manufacturer = manufacturer;
        this.price = price;
        this.hdCapacity = hdCapacity;
        this.ramCapacity = ramCapacity;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public double getPrice() {
        return price;
    }

    public int getHdCapacity() {
        return hdCapacity;
    }

    public int getRamCapacity() {
        return ramCapacity;
    }
}
  

结果:

Manufacturer: HP, Price: $400.0, Hard Drive: 100GB, RAM: 6GB
Manufacturer: HP, Price: $800.0, Hard Drive: 80GB, RAM: 8GB
Manufacturer: Dell, Price: $300.0, Hard Drive: 150GB, RAM: 6GB
Manufacturer: Dell, Price: $680.0, Hard Drive: 100GB, RAM: 8GB
Manufacturer: Toshiba, Price: $350.0, Hard Drive: 60GB, RAM: 4GB