对于我的程序,我有一个使用布尔语句的静态类。我想在用户赢得或输掉游戏的情况下在我的主变量中使用此变量。

但是,如果我尝试引用该变量,则会说找不到该变量。

这是我的代码的一小部分

    boolean playerWin;



    Dice.playerWin = false;


为何找不到该符号?

谢谢。

编辑:

    class Dice
    {

       static NumberFormat fmt = NumberFormat.getCurrencyInstance();

       public static String playRound(double playerBet)
       {
            boolean playerWin;
            double amountWon = 0;
            if(playerWin = false)
            {
                Wallet.playerBalance -= playerBet;

                amountWon = 0;

                return fmt.format(amountWon);
            }
            else
            {
                Wallet.playerBalance *= 2;

                amountWon = 1d/2d * Wallet.playerBalance;

                return fmt.format(amountWon);
            }
        }


在主要班级

    public class Game
    {
        public static void main(String[] args)
        {
             String playerName;
             int playerBet;

             Dice die = new Dice();

             System.out.print("How much would you like to bet? ");
             while(playerBet != -1 && playerBet > 0)
             {
                  playerDie.roll();
                  playerDie2.roll();
                  computerDie.roll();
                  computerDie2.roll();
                  if(computerDie.equals(computerDie2));
                  {
                       System.out.print("Sorry you lost");

                       Dice.playerWin = false;
                  }

                  System.out.println();
                  System.out.print("How much would you like to bet on this round? ");
                  playerBet = in.nextInt();


该变量在静态类中,并且在main方法中使用,我在做什么错?

希望这额外的帮助。

最佳答案

Static classStatic variable不同。
static class只能是nested class,并且仅表示它可以不存在instanceparent class而存在。
static variable是完全不同的概念。您需要将变量设置为static,仅具有static class是不够的。

10-07 19:33
查看更多