如何使用多个变量构建随机对象?

时间:2015-04-26 15:37:10

标签: java

这是我的主要计划。我想在这里实现的是每个" HeadPhones"设置为随机生成的颜色和制造商。目前,当程序运行时,除了我预设的变量之外,变量都是相同的。

package testheadphones;

import java.util.Random;

public class TestHeadPhones {



public static void main(String[] args) {

    String[] manufacturers = {"Skullcandy", "Klipsch", "Grado", "Shure",
    "Bose", "Beats", "Sony"};
    String[] colors = {"blue", "black", "green", "red", "purple", "pink",
    "white", "orange", "yellow"};



    Random random = new Random();
    int randColor = random.nextInt(2);
    String color = colors[randColor];
    int randMan = random.nextInt(2);
    String manufacturer = manufacturers[randMan];


    //Build three headphone sets.
    //Each set is encased in "{}" just for organization
    HeadPhones set1 = new HeadPhones();{
    set1.setIsPluggedIn(true);
    set1.getManufacturer();
    set1.setManufacturer(manufacturer);
    set1.getVolume();
    set1.setVolume(2);
    set1.getColor();
    set1.setColor(color);
    }

    HeadPhones set2 = new HeadPhones();{
    set2.setIsPluggedIn(true);
    set2.getManufacturer();
    set2.setManufacturer(manufacturer);
    set2.getVolume();
    set2.setVolume(1);
    set2.getColor();
    set2.setColor(color);
    }

    HeadPhones set3 = new HeadPhones();{
    set3.setIsPluggedIn(true);
    set3.getManufacturer();
    set3.setManufacturer(manufacturer);
    set3.getVolume();
    set3.setVolume(3);
    set3.getColor();
    set3.setColor(color);
    }

    System.out.println(set1.toString());
    System.out.println();
    System.out.println(set2.toString());
    System.out.println();
    System.out.println(set3.toString());

}

}

这是我的班级文件。

    package testheadphones;

import java.util.Random;

public class HeadPhones {

//Constant Declarations
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;

//Private Declarations
private int volume = MEDIUM;
private boolean pluggedIn = false;
private String manufacturer = null;
private String color = null;

Random random = new Random();

//Default Constructor
public void HeadPhones (int volume, String manufacturer, String Color){
    pluggedIn = false;
    manufacturer = "Sony";
    color = "red";
    volume = MEDIUM;
}

//Getter and Setter
public int getVolume(){
    return volume;
}
public void setVolume(int volume){
    this.volume = volume;
}
public String getManufacturer(){
    return manufacturer;
}
public void setManufacturer(String manufacturer){
    this.manufacturer = manufacturer;
}
public boolean getIsPluggedIn(){
    return pluggedIn;
}
public void setIsPluggedIn(boolean pluggedIn){
    this.pluggedIn = pluggedIn;
}
public String getColor(){
    return color;
}
public void setColor(String color){
    this.color = color;
}

public String toString(){
    String plugIn = this.pluggedIn == true ? "plugged in. " : "not plugged"
            + " in. ";

    String earBuds = "Your headphones are " + plugIn + "They are: " + 
            this.color + " " + this.manufacturer + " and are turned up to "
   + this.volume;

    return earBuds;
}

}

5 个答案:

答案 0 :(得分:2)

由于问题不明确,我会试着理解你想要的东西。

我想你想在整个数组中找到一个随机元素,而不仅仅是2个第一个元素:random.nextInt(colors.length);random.nextInt(manufacturers.length);而不是random.nextInt(colors.length);

您创建一个整数,并将其用于3个耳机。您可能想为每个耳机选择一种新颜色和一个新的制造商。然后将setX.setManufacturer(manufacturer);setX.setColor(color);替换为:

setX.setManufacturer(manufacturers[random.nextInt(manufacturers.length)]);
setX.setColor(colors[random.nextInt(colors.length)]);

答案 1 :(得分:0)

可能我看到你的一个错误是generating random number between 0 (inclusive) and the specified value i.e 2(exclusive),即只有两个值是generated 1 and 0。你需要在更多范围之间生成它

解决方法

Random random = new Random();
    int randColor = random.nextInt(manufacturers.length);
    String color = colors[randColor];
    int randMan = random.nextInt(colors.length);

答案 2 :(得分:0)

当你说

    int randMan = random.nextInt(2)

你说Random类生成0到1之间的随机int(2不包括)。 您可能想要做的是生成0到

之间的随机数
    manufacturers.length

同样适用于颜色。所以你需要的是:

    int randColor = random.nextInt(colors.length);
    int randMan = random.nextInt(manufacturers.length);

答案 3 :(得分:0)

这就是你想要的一切:

public static void main(String[] args) {
    String[] manufacturers = {"Skullcandy", "Klipsch", "Grado", "Shure", "Bose", "Beats", "Sony"};  
    String[] colors = {"blue", "black", "green", "red", "purple", "pink", "white", "orange", "yellow"};

    ArrayList<HeadPhones> list = new ArrayList<HeadPhones>();

    Random random = new Random();

    for(int i = 0; i < 10; i++){
        int volume = i + 1;

        int randManufacturers = random.nextInt(manufacturers.length);
        String manufacturer = manufacturers[randManufacturers];

        int randColor = random.nextInt(colors.length);
        String color = colors[randColor];

        HeadPhones headPhones = new HeadPhones(volume, manufacturer, color);

        list.add(headPhones);
    }

    //Print it
   foreach(HeadPhones headPhones: list){
       System.out.println(headPhones.toString());
   }

}

答案 4 :(得分:0)

您为所有自己的Headphone对象创建了一组随机变量值。也许您应该尝试创建可以为您提供适当随机变量的方法,例如:

public static String randomColor(){
    Random random = new Random();
    String[] colors = {"blue", "black", "green", "red", "purple", "pink",
            "white", "orange", "yellow"};
    int randColor = random.nextInt(colors.length);
    return colors[randColor];
}

然后你可以打电话给例如:

headphone.setColor(randomColor());

并轻松地在您想要的地方重复使用。