【发布时间】:2014-01-05 23:55:17
【问题描述】:
它找不到我的 Player 对象,但我声明了它们...当我尝试将卡片添加到手牌时,如何解决我的错误?
这是主类的相关部分:
while (something2.equals("yes") || playercount < 2) //Add players to game
{
System.out.println("Would a(nother) player like to join?");
something2 = scan.nextLine();
System.out.println();
if (something2.equals("yes"))
{
if (playercount <= 4)
{
if (playercount == 0)
{
System.out.println("What is your name: ");
Player one = new Player(scan.nextLine());
playercount++;
System.out.println();
}
else if (playercount == 1)
{
System.out.println("What is your name: ");
Player two = new Player(scan.nextLine());
playercount++;
System.out.println();
}
else if (playercount == 2)
{
System.out.println("What is your name: ");
Player three = new Player(scan.nextLine());
playercount++;
System.out.println();
}
else if (playercount == 3)
{
System.out.println("What is your name: ");
Player four = new Player(scan.nextLine());
playercount++;
System.out.println();
}
else {System.out.println("Only four players are allowed.");
something2 = "no";}
}
}
else if (playercount < 2)
{
System.out.println("You need at least two players...");
System.out.println();
}
else something2 = "no";
}
//Deal cards
if (playercount == 2)
{
for (int i = 1; i < 8; i++)
{
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
else if (playercount == 3)
{
for (int i = 1; i < 8; i++)
{
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
three.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
else
{
for (int i = 1; i < 8; i++)
{
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
three.addCard(Card.draw(deck));
deck = Card.getDeck();
four.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
}
还有我的播放器类:
import java.util.*;
public class Player
{
private static String name;
private static Card[] hand = new Card[52];
private static int handsize = 0;
//Constructor
public Player(String n)
{
name = n;
}
//Mutators
public static void addCard(Card c)
{
hand[handsize] = c;
handsize++;
}
//Accessors
public static String getName()
{
return name;
}
public static Card[] getHand()
{
return hand;
}
}
感谢您的帮助,如果您需要,我可以从我的课程中提供更多代码。
【问题讨论】:
-
好的,我们都知道问题所在。但是你到底想在这里完成什么。一个简单的全局声明它们可能无法解决问题,所以在我知道你试图用你的程序完全完成什么之前,我不会建议这样做。
-
我正在尝试将卡片添加到我创建的玩家手中,但它们不在我需要的范围内。