我无法将 (String-) 值传递给另一个 class 的 setter 方法
I cannot pass a (String-)value to another class's setter method
我知道我还没有写catch块。(原因?,但我认为这实际上不是问题; "Game" class的属性是完全可变的)
当我尝试调用 Player 中的 setName 方法时,我总是得到 IOException(即使我将 Player 中的 "name" 设置为 public 并直接更改它)。
public class game{
protected static int amountPlayers;
protected static Player[] playerList = new Player[amountPlayers];
public static void main (String[] args) throws IOException{
//Scanner reader = new Scanner(System.in);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String input;
System.out.println("new round? (1 for yes; enter for no):");
int boo = Integer.parseInt(br.readLine());
if (boo == 1) {
Rounds.setNew(true);
} // end of if
if (Rounds.getNew() == true) {
//SavingManagement.createFile();
System.out.println("# of players:");
int amount = Integer.parseInt(br.readLine());
setAmountPlayers(amount);
} // end of if
for (int i = 0; i < amountPlayers; i++) {
System.out.println("Name player No. " + (i + 1) + ":");
input = br.readLine();
playerList[i].setName(input);
} // end of for
}
public class Player {
protected static int score;
protected static String name = "";
public static void setName(String input) {
name = input;
}
}
PlayerList
包含 Player 对象,因此当您像这样调用 setName
方法时: playerList[i].setName(input)
它是通过 class Player 的一个实例,但是方法实际上是静态的,应该这样调用:
Player.setName()
尽管如此,您可以做的最好的事情是在 class Player
中添加一个构造函数,在数组 playerList
中添加新的 Player 对象并创建方法 setName()
class Player 中的其他变量是非静态的。
您需要播放器 class 作为 public 内部 class 吗?
您需要保护乐谱和名称吗?
否则这应该有效:
public class game {
protected static int amountPlayers;
protected static Player[] playerList = new Player[amountPlayers];
public static void main(String[] args) throws IOException {
for (int i = 0; i < amountPlayers; i++) {
playerList[i].setName("test");
}
}
}
class Player {
private int score;
private String name = "";
public void setName(String input) {
name = input;
}
}
假设您在 amountPlayers
中提供了有效大小,通过编写以下语句,您只是在创建播放器数组而不是对其进行初始化。
protected static int amountPlayers = 100;
/* This will just create the array */
protected static Player[] playerList = new Player[amountPlayers];
在使用 setName()
之前,您必须按如下方式初始化数组:
for(int x = 0; x < amountPlayers; x++) {
playerList[x] = new Player();
}
或者你可以这样做:
/* Create a new object of class Player */
Player myPlayer = new Player();
/* Set Name */
myPlayer.setName(input);
/* Assign it to your array */
playerList[i] = myPlayer;
我知道我还没有写catch块。(原因?,但我认为这实际上不是问题; "Game" class的属性是完全可变的)
当我尝试调用 Player 中的 setName 方法时,我总是得到 IOException(即使我将 Player 中的 "name" 设置为 public 并直接更改它)。
public class game{
protected static int amountPlayers;
protected static Player[] playerList = new Player[amountPlayers];
public static void main (String[] args) throws IOException{
//Scanner reader = new Scanner(System.in);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String input;
System.out.println("new round? (1 for yes; enter for no):");
int boo = Integer.parseInt(br.readLine());
if (boo == 1) {
Rounds.setNew(true);
} // end of if
if (Rounds.getNew() == true) {
//SavingManagement.createFile();
System.out.println("# of players:");
int amount = Integer.parseInt(br.readLine());
setAmountPlayers(amount);
} // end of if
for (int i = 0; i < amountPlayers; i++) {
System.out.println("Name player No. " + (i + 1) + ":");
input = br.readLine();
playerList[i].setName(input);
} // end of for
}
public class Player {
protected static int score;
protected static String name = "";
public static void setName(String input) {
name = input;
}
}
PlayerList
包含 Player 对象,因此当您像这样调用 setName
方法时: playerList[i].setName(input)
它是通过 class Player 的一个实例,但是方法实际上是静态的,应该这样调用:
Player.setName()
尽管如此,您可以做的最好的事情是在 class Player
中添加一个构造函数,在数组 playerList
中添加新的 Player 对象并创建方法 setName()
class Player 中的其他变量是非静态的。
您需要播放器 class 作为 public 内部 class 吗? 您需要保护乐谱和名称吗?
否则这应该有效:
public class game {
protected static int amountPlayers;
protected static Player[] playerList = new Player[amountPlayers];
public static void main(String[] args) throws IOException {
for (int i = 0; i < amountPlayers; i++) {
playerList[i].setName("test");
}
}
}
class Player {
private int score;
private String name = "";
public void setName(String input) {
name = input;
}
}
假设您在 amountPlayers
中提供了有效大小,通过编写以下语句,您只是在创建播放器数组而不是对其进行初始化。
protected static int amountPlayers = 100;
/* This will just create the array */
protected static Player[] playerList = new Player[amountPlayers];
在使用 setName()
之前,您必须按如下方式初始化数组:
for(int x = 0; x < amountPlayers; x++) {
playerList[x] = new Player();
}
或者你可以这样做:
/* Create a new object of class Player */
Player myPlayer = new Player();
/* Set Name */
myPlayer.setName(input);
/* Assign it to your array */
playerList[i] = myPlayer;