简体   繁体   中英

How do i assign and obtain values to objects created in an array?

Currently learning Java as a hobby and am making small game projects to reinforce the concepts. So for this one, I've made a method that creates an array of objects, in this case, a "Computer" object. I'm doing this because I want the user to decide at startup how many computer opponents they want to play against, instead of hard coding a set number of them. Now I want to assign and retrieve a value for each Computer object. For example a Computer name, bet amount, and a dice roll guess.

public class Computer {
    static int bet;
    static int guess;
    int cash;
    static Computer[] c;

    public static void create(int numComps) {

        c = new Computer[numComps];

        for (int i = 0; i < numComps; i++) {
            c[i] = new Computer();
            c[i].cash = Game.startCash;
            c[i].bet = bet();
            c[i].guess = guess();
            c[i].display();
        }
    }

    public static int bet() {
        bet = Rng.rand(Game.startCash / 50) * 50;
        return bet;

    }

    public static int guess() {
        guess = Dice.roll();
        return guess;

    }

    public void display() {
        String name = "Computer ";
        System.out.println("My name is " + name + " i bet " + bet + " and guess " + guess);
    }
}

When i do Computer.create(5) i get

My name is Computer  i bet 150 and guess 9
My name is Computer  i bet 50 and guess 3
My name is Computer  i bet 450 and guess 11
My name is Computer  i bet 250 and guess 11
My name is Computer  i bet 50 and guess 10

This output gives the appearance of working but i don't think i'm on the right track. For the name i want the syntax to be something like, name = "Computer " + c[i]. Resulting in "Computer 1", "Computer 2", "Computer 3" etc, not sure how to do that correctly. And an individual bet and guess to be assigned to each individual object. Right now i think its just displaying a random number rather than assigning that value to the particular object.

The bet and guess member variables shouldn't be static.

To display the id you can add a new int member variable, set it to i for each computer when you initialize them in the loop, and update the display() method to print it.

public class Computer {
    int id;
    int bet;
    int guess;
    int cash;
    static Computer[] c;

    public static void create(int numComps) {
        c = new Computer[numComps];

        for (int i = 0; i < numComps; i++) {
            c[i] = new Computer();
            c[i].id = i;
            c[i].cash = Game.startCash;
            c[i].bet = bet();
            c[i].guess = guess();
            c[i].display();
        }
    }

    public static int bet() {
        return Rng.rand(Game.startCash / 50) * 50;
    }

    public static int guess() {
        return Dice.roll();
    }

    public void display() {
        String name = "Computer ";
        System.out.println("My name is " + name + id + " bet " + bet + " and guess " + guess);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM