在 类 中使用静态访问修饰符
Utilizing Static Access Modifier in Classes
我正在尝试设计像 yahtzee 这样的两人骰子游戏。在主要 class 中,我实例化了两名球员和两张记分卡,每个球员一张。然后我循环遍历每个玩家的回合。每个玩家最多掷 3 次五个骰子,以做出某些组合。在方法 playerTurn() 中,在我尝试访问 class 记分卡的最后两行中,它没有访问正确的记分卡。
例如:如果玩家一完成他们的回合并掷出 4 个,并在方法 getCategory() 中选择了一个类别,该方法将设置 ones = 为 4。然后当调用 printScorecard() 时,记分卡将打印每个category 设置为 0,但 ones 将被设置为 4。在玩家 2 完成他们的回合并掷出 3 个 twos(等于 6 分)后,并在方法 getCategory() 中选择接受两个类别,该方法将将 twos = 设置为 6。但是,当我尝试在方法 printScorecard() 中打印玩家二的记分卡时,它将打印 ones=4, twos=6, threes=0, fours=0, fives=0, sixes=0 .由于它是玩家二的记分卡,而玩家一是掷出 4 个的那个,它应该打印 ones=0。它将像这样继续将每个玩家的分数添加到同一个记分卡中。当我实例化了两个不同的记分卡时,为什么会发生这种情况?
这是主要内容 Class:
public class Main {
Random rand = new Random();
static Scanner sc = new Scanner( System.in );
public static void main(String[] args)
{
System.out.println("What is player one's name?");
String name1 = sc.next();
System.out.println("What is player two's name?");
String name2 = sc.next();
Player p1=new Player(name1);
Scorecard s1=new Scorecard();
Player p2=new Player(name2);
Scorecard s2=new Scorecard ();
for (int i=0; i<13; i++)
{
System.out.println(name1+"'s turn:");
playerTurn(p1, s1);
System.out.println(name2+"'s turn:");
playerTurn(p2, s2);
}
System.out.println("Game Over. Player one's score is " + s1.getScore());
System.out.println("Game Over. Player two's score is " + s2.getScore());
if (s1.getScore()>s2.getScore())
System.out.println("Player one wins!!!");
if (s2.getScore()>s1.getScore())
System.out.println("Player two wins!!!");
else
System.out.println("It's a tie.");
}
public static void playerTurn(Player player, Scorecard scorecard)
{
{
player.getCup().printDice();
for (int i=0; i<2; i++)
{
System.out.println("Do you want to roll again (y/n)?");
String answer =sc.next();
if (answer.equalsIgnoreCase("y"))
{
System.out.println("How many dice do you want to reroll?");
int roll=sc.nextInt();
for (int j=0; j<roll; j++)
{
System.out.println("Enter the number of a die (1-5) you want to reroll");
int num=sc.nextInt();
player.getCup().reroll(num);;
}
player.getCup().printDice();
}
if (answer.equalsIgnoreCase("n"))
break;
}
scorecard.getCategory(); //Here when I access scorecard it is not working
scorecard.printScoreCard();
}
}
这是 class 记分卡:
public class Scorecard
{
static Scanner sc = new Scanner( System.in );
private static int ones;
private static int twos;
private static int threes;
private static int fours;
private static int fives;
private static int sixes;
public Scorecard()
{
ones=00;
twos=00;
threes=00;
fours=00;
fives=00;
sixes=00;
}
public static void printScoreCard()
{
//System.out.println(Player.getName() +"'s Score Card:");
System.out.println(" *******Score Card*******");
System.out.println("Ones...................." +ones);
System.out.println("Twos...................."+twos);
System.out.println("Threes.................."+threes);
System.out.println("Fours..................."+fours);
System.out.println("Fives..................."+fives);
System.out.println("Sixes..................."+sixes);
System.out.println("************************");
}
public static void getCategory()
{
int selection=0;
int score=0;
System.out.println("Which category do you want to take (enter a number 1-6):");
if (ones==00)
System.out.println("1. Ones");
if (twos==00)
System.out.println("2. Twos");
if (threes==00)
System.out.println("3. Threes");
if (fours==00)
System.out.println("4. Fours");
if (fives==00)
System.out.println("5. Fives");
if (sixes==00)
System.out.println("6. Sixes");
selection=sc.nextInt();
if (selection==1)
{
score= addCertainDice(1);
ones=score;
}
else if (selection==2)
{
score= addCertainDice(2);
twos=score;
}
else if (selection==3)
{
score= addCertainDice(3);
threes=score;
}
else if (selection==4)
{
score= addCertainDice(4);
fours=score;
}
else if (selection==5)
{
score= addCertainDice(5);
fives=score;
}
else if (selection==6)
{
score= addCertainDice(6);
sixes=score;
}
我没有读完整个问题,但你犯了一个基本错误:记分卡 class 中的状态是静态变量。这些在 class 的所有实例之间共享。因此,从那里和想要访问它们的方法中删除 static
关键字。
注意:新程序员遇到这种情况的一种方法是声明他们的方法 static
,然后编译器或 Eclipse 抱怨静态方法无法访问实例变量,然后 Eclipse 提供通过使变量 static
。这是错误的建议,虽然它使代码编译。
当您将字段声明为 static
时,这意味着该字段有一个副本,而不是每个实例一个。
这意味着 Scorecard
的 getCategory
和 printScoreCard
方法(以及您以后添加的任何其他方法)访问相同的字段,无论您在哪个记分卡上调用它们。
解决方案是让这些字段不 static
。
您还没有发布您的 Player
class,但您应该检查它是否有同样的问题。
我正在尝试设计像 yahtzee 这样的两人骰子游戏。在主要 class 中,我实例化了两名球员和两张记分卡,每个球员一张。然后我循环遍历每个玩家的回合。每个玩家最多掷 3 次五个骰子,以做出某些组合。在方法 playerTurn() 中,在我尝试访问 class 记分卡的最后两行中,它没有访问正确的记分卡。 例如:如果玩家一完成他们的回合并掷出 4 个,并在方法 getCategory() 中选择了一个类别,该方法将设置 ones = 为 4。然后当调用 printScorecard() 时,记分卡将打印每个category 设置为 0,但 ones 将被设置为 4。在玩家 2 完成他们的回合并掷出 3 个 twos(等于 6 分)后,并在方法 getCategory() 中选择接受两个类别,该方法将将 twos = 设置为 6。但是,当我尝试在方法 printScorecard() 中打印玩家二的记分卡时,它将打印 ones=4, twos=6, threes=0, fours=0, fives=0, sixes=0 .由于它是玩家二的记分卡,而玩家一是掷出 4 个的那个,它应该打印 ones=0。它将像这样继续将每个玩家的分数添加到同一个记分卡中。当我实例化了两个不同的记分卡时,为什么会发生这种情况?
这是主要内容 Class:
public class Main {
Random rand = new Random();
static Scanner sc = new Scanner( System.in );
public static void main(String[] args)
{
System.out.println("What is player one's name?");
String name1 = sc.next();
System.out.println("What is player two's name?");
String name2 = sc.next();
Player p1=new Player(name1);
Scorecard s1=new Scorecard();
Player p2=new Player(name2);
Scorecard s2=new Scorecard ();
for (int i=0; i<13; i++)
{
System.out.println(name1+"'s turn:");
playerTurn(p1, s1);
System.out.println(name2+"'s turn:");
playerTurn(p2, s2);
}
System.out.println("Game Over. Player one's score is " + s1.getScore());
System.out.println("Game Over. Player two's score is " + s2.getScore());
if (s1.getScore()>s2.getScore())
System.out.println("Player one wins!!!");
if (s2.getScore()>s1.getScore())
System.out.println("Player two wins!!!");
else
System.out.println("It's a tie.");
}
public static void playerTurn(Player player, Scorecard scorecard)
{
{
player.getCup().printDice();
for (int i=0; i<2; i++)
{
System.out.println("Do you want to roll again (y/n)?");
String answer =sc.next();
if (answer.equalsIgnoreCase("y"))
{
System.out.println("How many dice do you want to reroll?");
int roll=sc.nextInt();
for (int j=0; j<roll; j++)
{
System.out.println("Enter the number of a die (1-5) you want to reroll");
int num=sc.nextInt();
player.getCup().reroll(num);;
}
player.getCup().printDice();
}
if (answer.equalsIgnoreCase("n"))
break;
}
scorecard.getCategory(); //Here when I access scorecard it is not working
scorecard.printScoreCard();
}
}
这是 class 记分卡:
public class Scorecard
{
static Scanner sc = new Scanner( System.in );
private static int ones;
private static int twos;
private static int threes;
private static int fours;
private static int fives;
private static int sixes;
public Scorecard()
{
ones=00;
twos=00;
threes=00;
fours=00;
fives=00;
sixes=00;
}
public static void printScoreCard()
{
//System.out.println(Player.getName() +"'s Score Card:");
System.out.println(" *******Score Card*******");
System.out.println("Ones...................." +ones);
System.out.println("Twos...................."+twos);
System.out.println("Threes.................."+threes);
System.out.println("Fours..................."+fours);
System.out.println("Fives..................."+fives);
System.out.println("Sixes..................."+sixes);
System.out.println("************************");
}
public static void getCategory()
{
int selection=0;
int score=0;
System.out.println("Which category do you want to take (enter a number 1-6):");
if (ones==00)
System.out.println("1. Ones");
if (twos==00)
System.out.println("2. Twos");
if (threes==00)
System.out.println("3. Threes");
if (fours==00)
System.out.println("4. Fours");
if (fives==00)
System.out.println("5. Fives");
if (sixes==00)
System.out.println("6. Sixes");
selection=sc.nextInt();
if (selection==1)
{
score= addCertainDice(1);
ones=score;
}
else if (selection==2)
{
score= addCertainDice(2);
twos=score;
}
else if (selection==3)
{
score= addCertainDice(3);
threes=score;
}
else if (selection==4)
{
score= addCertainDice(4);
fours=score;
}
else if (selection==5)
{
score= addCertainDice(5);
fives=score;
}
else if (selection==6)
{
score= addCertainDice(6);
sixes=score;
}
我没有读完整个问题,但你犯了一个基本错误:记分卡 class 中的状态是静态变量。这些在 class 的所有实例之间共享。因此,从那里和想要访问它们的方法中删除 static
关键字。
注意:新程序员遇到这种情况的一种方法是声明他们的方法 static
,然后编译器或 Eclipse 抱怨静态方法无法访问实例变量,然后 Eclipse 提供通过使变量 static
。这是错误的建议,虽然它使代码编译。
当您将字段声明为 static
时,这意味着该字段有一个副本,而不是每个实例一个。
这意味着 Scorecard
的 getCategory
和 printScoreCard
方法(以及您以后添加的任何其他方法)访问相同的字段,无论您在哪个记分卡上调用它们。
解决方案是让这些字段不 static
。
您还没有发布您的 Player
class,但您应该检查它是否有同样的问题。