它找不到我的玩家物品,但我声明.当我试图在手边添加卡片时,什么将修复我的错误?
以下是主要课程的相关部分:
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;
}
}我感谢任何帮助,如果您需要,我可以从我的类中提供更多的代码。
发布于 2013-12-18 00:25:19
用大括号分隔的每一个代码块{}都定义了一个作用域。在该作用域中声明的任何命名实体只有在声明后才能在该范围内访问。
您已经在自己的Player块作用域中声明了每个if变量。除了这些之外,它们是无法访问的。
或者在更大的范围内声明它们,例如,在if块之外,或者对块内的对象做所有您需要做的事情。
下面是对这一现象的另一个描述。
https://stackoverflow.com/questions/20647394
复制相似问题