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

Your Ally :

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

glglgl :

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.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I create objects at runtime?

How do I create an boolean equals method compares objects in an array

How do I merge an array of objects in Javascript?

How do i push objects into the same array?

How do I create a react list component from an array of objects?

How do I filter on an array of objects in Swift?

How do I group by identifier and create an array of JSON objects in postgreSQL?

How do I create an array of partial objects from another array?

How Do I Save an Array of Objects to NSUserDefaults?

How do I create a javascript array of lists of objects?

How do I create a vector of objects in R?

How is this JavaScript pattern called and how do I use it in the right way?

How to create route in right way?

How do I create automatic div for objects?

How to Create the right array?

how do I create an array of objects with array in the object from an object with the same key in javascript

How can I do array of objects

How do I create multiple objects in Java?

How do I create an Array of Objects in C?

How do I create an array of inherited class objects?

What is the right way to pass array of objects with redirect?

How do I format an array of objects with ruby?

How do I create an flattened array of arrays that are part of an object in an array, essentially get the child arrays of multiple objects?

How do I create a list of objects in Kotlin?

How do I create objects from lists?

How can I do fast sort of big objects array, sime Best way? (100Mb size)

How do I create an object from array of objects using javascript?

How do I create an array of objects with a nested array based on a similar key?

How do I create new arrays of objects with objects from an existing array, based on values in the objects?