我創建了一個簡單遊戲的玩家陣容。我需要在控制檯上顯示它正在播放的內容。我有一個班級「玩家」,我存儲名稱和分數。也是爲每個玩家定義這些值的構造函數。無法弄清楚如何從ArrayList中的索引中的球員只有名稱..如何從僅知道索引ArrayList,Java的對象中獲取一個值?
package Øving5;
import java.util.ArrayList;
import java.util.Scanner;
public class Game {
public static void Round() {
ArrayList<Player> players = new ArrayList<Player>();
Scanner in = new Scanner(System.in);
System.out.print("Write down the number of players: ");
int numplayers = in.nextInt();
for (int i = 0; i < numplayers; i++)
players.add(new Player());
for (int i = 0; i < numplayers; i++)
theplayer = players.get(i); // Don't know what to do here..
System.out.print("It is " + toString(theplayer) + "'s turn.");
}
public static void main(String[] args) {
Round();
}
}