因此,我在名为 Player 的类中设置了构造函数,其中每个玩家都是一个 5 的数组(将他们的赌注存储在另一个类中):
public class Player {
private int[] anyplayer = new int[5];
// constructor for each player at the table
public Player(int[] anyplayer) {
this.anyplayer = anyplayer;
}
现在我已经在不知不觉中将我的另一个类编码为:
int[] player2 = new int[5]; // this is a variable I'm trying to change to the object
try{
for(int i=0;i<5;i++) {
player2[i] = Integer.parseInt(keyin.nextLine());
}
}
catch(NumberFormatException e){
System.out.println("Player 3 : ");
}
现在我似乎无法在不破坏一切的情况下找到解决方法!我仍然是编程新手,但是有没有一种简单的方法可以将 int[] player2 = new int[5];
替换为构造函数中的实际对象,例如 Player player2 = 新玩家();
?
我已经尝试过,但它一直显示构造函数 Player() 未定义
最佳答案
你的构造函数接受一个整数数组,所以它将是
int[] player2 = new int[5];
Player somePlayer = new Player(player2);
关于java - 用 Java 对象替换变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471430/