在类中使用静态访问修饰符

时间:2015-02-03 21:17:43

标签: java class

我正在尝试设计像yahtzee这样的双人骰子游戏。在主课程中,我实例化了两个玩家和两个记分卡,每个玩家一个。然后我循环遍历每个玩家的回合。每个玩家掷五个骰子最多三次,以便进行某些组合。在方法playerTurn()中,在我尝试访问类记分卡的最后两行中,它没有访问正确的记分卡。 例如:如果玩家一轮完成轮次并滚动4个并且选择在方法getCategory()中选择一个类别,则该方法将ones =设置为4.然后当调用printScorecard()时,记分卡将打印出来类别设置为0但是一些,将设置为4.在玩家2完成转弯并滚动3个二等级(等于6个点)后,并选择在方法getCategory()中取两个类别,该方法将设置twos =到6.但是,当我尝试在方法printScorecard()中打印玩家二的记分卡时,它会打印一个= 4,两个= 6,三个= 0,四个= 0,五个= 0 ,sixes = 0。因为它是玩家2的记分卡而玩家1是滚动4个记分卡,所以它应该打印1 = 0。它会继续将每个玩家分数添加到同一个记分卡中。当我实例化两个不同的记分卡时,为什么会发生这种情况?

这是主类:

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

        }
    }

这是课程记分卡:

     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;
        }

2 个答案:

答案 0 :(得分:1)

我没有阅读整个问题,但你犯了一个基本错误:Scorecard类中的状态是静态变量。这些在班级的所有实例之间共享。因此,请删除那里的static关键字以及要访问它们的方法。

注意:新程序员遇到这种情况的一种方法是声明他们的方法static,然后编译器或Eclipse抱怨静态方法无法访问实例变量,然后Eclipse提供修复它的方法变量static。这是错误的建议,虽然它使代码编译。

答案 1 :(得分:1)

当您将字段声明为static时,这意味着该字段有一个副本,而不是每个实例一个。

这意味着getCategory的{​​{1}}和printScoreCard方法(以及之后添加的任何其他方法)都可以访问相同的字段,无论您调用哪个记分卡。

解决方案是使这些字段不是Scorecard

您尚未发布static课程,但应检查是否存在同样的问题。