无法将数组列表分配给Object(Java)数组

时间:2018-03-31 02:12:11

标签: java

所以我试图达成交易或没有交易游戏游戏还没有完成,但我遇到的最大问题是,当我试图将数组列表分配给一系列类型的情况时,似乎它不是& #39;被分配。

我试图调试并且在shuffle之后输出是正确的但是我无法将结果分配给case数组以便我可以在游戏中使用它

以下是我分配结果的3个课程

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 class Player {

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 class 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();

}

}

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:-1)

您获得这些奇怪值的原因不是因为分配不起作用,而是因为您没有打印值的字符串值。请尝试以下方法。

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