gpt4 book ai didi

java - 无法将数组列表分配给对象数组(Java)

转载 作者:行者123 更新时间:2023-12-01 06:04:04 25 4
gpt4 key购买 nike

所以我正在尝试进行交易或不交易游戏,游戏尚未完成,但我遇到的最大问题是,当我尝试将数组列表分配给类型案例数组时,它似乎没有完成没有被分配。

我尝试调试,洗牌后输出正确,但我无法将结果分配给案例数组,以便我可以在游戏中使用它

下面是我分配的 3 门类(class),我得到的结果是

console output

我所说的这一行是可用案例的方法

公开课案例{

private int value = 0;
private String face;


/*
* Constructor for type Case
*/

public Case(int value)
{
this.value = value;
}

/*
* Getter and setter methods for instance data
*/

public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getFace() {
return face;
}
public void setFace(String face) {
this.face = face;
}

}

公共(public)类玩家{

private String name;
private int age;
private boolean canPlay = false;
private int money = 0;

/*
* Constructor for type Player
*/
public Player(String name, int age) {
super();
this.name = name;
this.age = age;
}

/*
* Getter and Setter methods for all instance Data
*/

public Player(boolean canPlay)
{
this.canPlay = canPlay;
}

public int getMoney() {
return money;
}

public void setMoney(int money) {
this.money = money;
}

public boolean isCanPlay() {
return canPlay;
}
public void setCanPlay(boolean canPlay) {
this.canPlay = canPlay;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

/*
* This method will check if the person playing is at least 18 years old or not
*/
public void checkAge()
{
if(age >= 18)
{
canPlay = true;
System.out.println("Seems like you are old enough to play!");
System.out.println("Let's get started");
}
else
{
canPlay = false;
System.out.println("OH NO! you aren't old enough sadly we won't be able to continue");
}
}

public String toString() {
return "Today's player is "+name+" who is "+age+" old";
}

public static void setupPlayer()throws InputMismatchException
{
String playerName;
int playerAge;

System.out.println("Welcome to the Deal or No Deal game!");
System.out.println("Please state your name:");
Scanner name = new Scanner(System.in);
playerName = name.nextLine();

System.out.println("Welcome "+playerName+" how old are you?");
Scanner age = new Scanner(System.in);
playerAge = age.nextInt();

Player gamePlayer = new Player(playerName, playerAge);
}

public static void Rules()
{
System.out.println("Below listed are the Game Rules\n");
System.out.println("-There are 12 Cases in the game");
System.out.println("-Each case contains a amount of money and you will be "
+ "offered these Cases 1 at a time");
System.out.println("-Upon picking a Case the game will end and you will have a "
+ "chance to walk away with that case");
System.out.println("-If No cases are picked you will get 2 option, to walk away"
+ " with the last Case or take the bankers offer");
System.out.println("-To accept the case type \"Y\" ,to decline it type \"N\"");
}

}

公共(public)类SetUpCases{

private  Case[] cases = new Case[12];

/*
* This method initializes each object type with an amount which will be the Money in each Case
*/
public void settingUpCases()
{
ArrayList<Integer> myCase= new ArrayList<Integer>();

myCase.add(new Integer(1));
myCase.add(new Integer(50));
myCase.add(new Integer(100));
myCase.add(new Integer(250));
myCase.add(new Integer(500));
myCase.add(new Integer(1000));
myCase.add(new Integer(2500));
myCase.add(new Integer(5000));
myCase.add(new Integer(10000));
myCase.add(new Integer(25000));
myCase.add(new Integer(50000));
myCase.add(new Integer(100000));

/*
* The Shuffle changes which index has what value so game results are different each time played!
*/
Collections.shuffle(myCase);

for(int i = 0; i < cases.length; i++)
{
int value = myCase.get(i);
cases[i] = new Case (value);
System.out.println(cases[i]);
}

}

/*
* Shows which Cases are still available
*/
public void availableCases()
{
for (int k = 0; k < cases.length; k++)
{
System.out.println(cases[k]);
}
}


public void startGame()
{
settingUpCases();

}

}

最佳答案

您正在打印 case 对象而不是其值。使用 getValue (或 getFace)方法打印 value (或 >脸)。例如

for (int k = 0; k < cases.length; k++)
{
System.out.println(cases[k].getValue());
}

如果您想同时打印 valueface,最好的方法是重写 toString 方法并在那里打印这些变量。

关于java - 无法将数组列表分配给对象数组(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49583555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com