Your Ally
Your Ally

Reputation: 3

How do I create an array of objects the right way?

So I'm just starting to use objects in Java and I heard you can make an array of objects, but I'm having some problems with it.

public class Minesweeper {

    static int Turns;
    static boolean Won;
    static String Name;
    static int Winnings;
    public Minesweeper(boolean won, int turns,String pName,int score){
        Turns=turns;
        Won=won;
        Name=pName;
        Winnings=score;

    }

    public static void main(String[] args) {

        Scanner inpt = new Scanner(System.in);
        int pl=0,again,respd;
        String ans;
        String pName;
        Minesweeper [] player= new Minesweeper[20];

        do{

        again=0;
        respd=0;

        System.out.println("What's your name?");
        pName=inpt.next();


        Won=false;
        Turns=0;    

        play(inpt);

        System.out.println("");
        System.out.println("Play Again?");

        ans = inpt.next();
        if(ans.charAt(0)=='y'||ans.charAt(0)=='Y'){
            again=1;
        }
        else{
            again=0;
        }

        player[pl]= new Minesweeper(Won,Turns,pName,Winnings);
        pl++;

        }while(again==1);

        System.out.println("Won?:\tName:\tTurns:\t:Score:");
        for(int i=0;i<pl;i++){
            System.out.println(player[i].Won+"\t"+player[i].Name+"\t"+player[i].Turns+"\t"+player[i].Winnings);
        }

    }

It just outputs the last player's score repeatedly (because of the loop), I want it to print every player that has played the game.

What are the lines do I have to change

The whole code https://pastebin.com/hX1kEYcQ

Upvotes: 0

Views: 76

Answers (2)

glglgl
glglgl

Reputation: 91017

The fields you intend to store the data for each player are static. This contradicts their intended use.

If you have 5 instances of your class, static int Turns; makes the field exist only once. Removing static gives every instance a separate field, which is what you need.

(BTW, field names in Java usually start with lower case, so better use turns instead of Turns.)

If you turn

static int Turns;
static boolean Won;
static String Name;
static int Winnings;

into

int turns;
boolean won;
String name;
int winnings;

you'll see that your main loop doesn't work any longer.

Thus, you have to add local variables in your main():

int turns = 0;
boolean won = false;
String name;
int winnings;

The won and turns values have to be obtained somehow as well in a clean way, as well as winnings. (Maybe you want to make them a separate class GameResult which is returned by play().)

Then you can do

    player[pl]= new Minesweeper(won,turns,pName,winnings);

(BTW, the name should maybe put first, but that's a matter of style.)

Upvotes: 2

mikeb
mikeb

Reputation: 11267

If you don't know the size ahead of time, I'd use a List instead of array:

List<Minesweeper> minesweeperList = new ArrayList<>();

Then, you can just:

minesweeperList.add(new Minesweeper(Won,Turns,pName,Winnings));

Then:

for(Minesweeper m : minesweeperList) {
    ...
}

Static values in Java mean there is only 1 value shared by all objects in your program. The way you have it written all Minesweeper objects share the same information for each static var. Change them to "instance" variables by removing the "static" keyword:

public class Minesweeper {

    int Turns;
    boolean Won;
    String Name;
    int Winnings;
    public Minesweeper(boolean won, int turns,String pName,int score){
        Turns=turns;
        Won=won;
        Name=pName;
        Winnings=score;

    }

Upvotes: 0

Related Questions