I wrote a program to generate a group of objects using a for-loop, and then send output to console. I have managed to make output readable. However the console output looks not so good.
Code for class "Player":
public class Player
{
static String Name;
static int HP;
static int ATK;
static int playerNum;
public Player()
{
System.out.println("Empty constructor called. ");
}
public Player(String Name, int HP, int ATK)
{
Player.Name = Name;
Player.HP = HP;
Player.ATK = ATK;
}
public StringBuilder setName()
{
StringBuilder conc = new StringBuilder();
StringBuilder result = conc.append("Player ").append(Name);
return result;
}
}
Code for class "Multiplayers"
public class Multiplayers
{
public static void createPlayer()
{
int playerCount;
int players [] = new int [2];
for (playerCount = 0; playerCount<players.length; playerCount++)
{
Player p1 = new Player("なのは", 100, 100);
StringBuilder p1_n = p1.setName();
System.out.print(p1_n+" ");
System.out.println(playerCount+1);
System.err.println("ATK "+Player.ATK);
System.out.println(Player.HP);
}
}
}
The main method only calls "createPlayer()" method. What I want console to display is this, which I can see it while debugging:
Player なのは 1
ATK 100
100
Player なのは 2
ATK 100
100
What I see after running is this:
Player なのは 1
ATK 100
100
ATK 100
Player なのは 2
100
Since the program can run nicely and I don't see abnormal result during debugging, I have no idea why output shows in different order. .