无法将数组列表分配给对象(Java)的数组
所以我正在尝试做一个交易或不交易的游戏,游戏还没有结束,但我遇到的最大问题是,当我试图将数组列表分配给一个类型案例数组时,它似乎没有被分配 我尝试过调试,洗牌后的输出是正确的,但我无法将结果分配给案例数组,以便我可以在游戏中使用它 下面是我的3门课,在分配结果时,我得到的结果是 我所说的这一行是可用案例的方法 公开课案例{无法将数组列表分配给对象(Java)的数组,java,Java,所以我正在尝试做一个交易或不交易的游戏,游戏还没有结束,但我遇到的最大问题是,当我试图将数组列表分配给一个类型案例数组时,它似乎没有被分配 我尝试过调试,洗牌后的输出是正确的,但我无法将结果分配给案例数组,以便我可以在游戏中使用它 下面是我的3门课,在分配结果时,我得到的结果是 我所说的这一行是可用案例的方法 公开课案例{ private int value = 0; private String face; /* * Constructor for type Case */ pub
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;
}
}
公开课选手{
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\"");
}
}
公营案件{
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();
}
private Case[]cases=新案例[12];
/*
*此方法使用在每种情况下都是货币的金额初始化每个对象类型
*/
公共无效设置案例()
{
ArrayList myCase=新的ArrayList();
添加(新的整数(1));
添加(新的整数(50));
添加(新的整数(100));
add(新的整数(250));
添加(新的整数(500));
添加(新的整数(1000));
添加(新整数(2500));
添加(新整数(5000));
添加(新整数(10000));
添加(新整数(25000));
添加(新整数(50000));
添加(新整数(100000));
/*
*洗牌会改变哪个索引有什么值,所以每次玩游戏的结果都不一样!
*/
集合。shuffle(myCase);
for(int i=0;i
}获取这些奇怪值的原因不是因为赋值不起作用,而是因为没有打印值的字符串值。尝试以下方法
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]));
}
for(int i=0;i
您正在打印案例对象,而不是其值。。使用getValue
(或getFace
)方法打印value
(或face
)。比如说
for (int k = 0; k < cases.length; k++)
{
System.out.println(cases[k].getValue());
}
for(int k=0;k
如果要同时打印
值
和面
,最好的方法是重写toString
方法并在那里打印这些变量。。。。我无法将结果分配给案例数组…-请清楚地标识您正在谈论的特定行。现在编辑帖子。输出清楚地显示您已将大小写
对象分配给数组。问题是你打印它们的方式看起来不太好。阅读PrintWriter::println(对象)
的javadoc。这将解释正在发生的事情,并且应该给你一些关于如何使你的代码工作的想法。这里的教训是你需要弄清楚证据实际上意味着什么。。。而不是仓促下结论,比如“任务不起作用”。我试过了,但没有成功。链接显示错误。非常感谢将我的方法更改为静态后,修复了该问题。