簡體   English   中英

如何從靜態類的main方法中引用變量? (java)

[英]How to reference a variable in main method from a static class? (java)

對於我的程序,我有一個使用布爾語句的靜態類。 我想在用戶贏得或輸掉游戲的情況下在我的主變量中使用此變量。

但是,如果我嘗試引用該變量,則會說找不到該變量。

這是我的代碼的一小部分

    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 ,並且僅表示它可以不帶parent class instance而存在。 static variable是完全不同的概念。 您需要將變量設置為static ,僅具有static class是不夠的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM