【问题标题】:Updating user balance method更新用户余额方式
【发布时间】:2018-04-23 04:13:33
【问题描述】:

在下面的代码中,我尝试增加或减少用户投注(例如 10 美元)并更新余额金额。最初,余额将从 100 美元开始,随着用户继续下注,余额将更新,直到余额达到 0 美元或用户决定退出。

下面的代码在第一次下注时可以正常工作,但在第二次下注时余额不会更新。任何帮助将不胜感激!

int balance = 100; 

    protected int PlayerBalance()
    {

        int totalBalance = 0;
        if (CalculateWinAmount() > 0) //CalculateWinAmount() method returns the win amount. If no wins, the method return 0
        {
            totalBalance += CalculateWinAmount();
        }
        else
        {
            totalBalance -= BetAmount(); //BetAmount() method returns the amount the user has bet (e.g. $5, $10..)
        }          
        return totalBalance += balance;
    }
    protected void DisplayPlayerBalance()
    {
        playerTotalAmountLabel.Text = PlayerBalance().ToString();
    }      

【问题讨论】:

    标签: c# asp.net .net oop methods


    【解决方案1】:

    您似乎没有设置平衡:尝试以下操作

    int balance = 100; 
    
    protected int PlayerBalance()
    {
    
        int totalBalance = 0;
        if (CalculateWinAmount() > 0) //CalculateWinAmount() method returns the win amount. If no wins, the method return 0
        {
            totalBalance += CalculateWinAmount();
        }
        else
        {
            totalBalance -= BetAmount(); //BetAmount() method returns the amount the user has bet (e.g. $5, $10..)
        }          
        // Set the balance to the current balance + the new totalbalance
        balance += totalBalance;
    
        // return the balance figure
        return balance;
    }
    protected void DisplayPlayerBalance()
    {
        playerTotalAmountLabel.Text = PlayerBalance().ToString();
    }   
    

    【讨论】:

    • 每次下注前,它仍会将余额重置为 100 美元。例如,在您的第一次下注中,您下注 5 美元并且您输了,余额将更新为 95 美元。但是,如果您再次下注 5 美元但输了,余额将更新为 95 美元而不是 90 美元。
    • 它不应该再次将余额重置为 100,除非您重新运行将余额设置为 100 的第一行。如果您只运行 PlayerBalance() 函数,它将起作用。
    【解决方案2】:

    @Mattew 已经在他的回答中指定了您没有正确设置余额。

    作为重构的一部分,您不需要局部变量totalBalance,因为您可以直接对balance 进行操作。并且出于性能原因,您不应两次调用CalculateWinAmount

    重构代码:

            static int balance = 100;
            protected int PlayerBalance()
            {
                int winAmount = CalculateWinAmount();//CalculateWinAmount() method returns the win amount. If no wins, the method return 0
                if (winAmount > 0) 
                {
                    balance += winAmount;
                }
                else
                {
                    int betAmount = BetAmount();
                    balance -= betAmount; //BetAmount() method returns the amount the user has bet (e.g. $5, $10..)
                }
                return balance;
            }
            protected void DisplayPlayerBalance()
            {
                playerTotalAmountLabel.Text = PlayerBalance().ToString();
            }
    

    【讨论】:

    • 每次下注前,它仍会将余额重置为 100 美元。例如,在您的第一次下注中,如果您下注 5 美元但输了,余额将更新为 95 美元。但是,如果您再次下注 5 美元但输了,余额将更新为 95 美元而不是 90 美元。
    • 因为 balance 是实例字段,每次创建实例时都会初始化为 100。您应该将 balance 变量定义为仅初始化一次的静态变量。
    • 谢谢你,阿卡什。静态是我一直缺少的关键字。再次感谢您的帮助。
    【解决方案3】:

    您从未更新 PlayerBalance 方法中的 balance 字段。

    我建议不要使用局部变量 totalBalance,而是简单地更新余额,如下所示:

    protected int PlayerBalance()
    {
        if (CalculateWinAmount() > 0) //CalculateWinAmount() method returns the win amount. If no wins, the method return 0
        {
            balance += CalculateWinAmount();
        }
        else
        {
            balance -= BetAmount(); //BetAmount() method returns the amount the user has bet (e.g. $5, $10..)
        }          
        return balance;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-08
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 2010-09-16
      • 2020-08-15
      相关资源
      最近更新 更多