轮盘赌游戏在循环时挂起

时间:2017-02-21 00:47:52

标签: c#

我想让这个while循环继续玩轮盘游戏,直到我的预算为空。当我运行代码时,它就会挂起。 这是while代码,包括挂起的循环。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    
    }

    protected void btnPlay_Click(object sender, EventArgs e)
    {          
        int Budget = Convert.ToInt32(txtMyBudget.Text);
        int Bet = Convert.ToInt32(txtMyBet.Text);
        int Outcome = Convert.ToInt32(txtMyNum.Text);

        //generates a random number between 0 and 38
        Random num = new Random();

        int numSpun = num.Next(0, 38);
        lblBudgetError.Text = numSpun.ToString();

        //checks to make sure the budget is set
        if (txtMyBudget.Text == "")
        {
            lblBudgetError.Text = "Please set your budget";
        }

        int myBudget = Budget;           
        while (Budget >= Bet)
        {
            myBudget++;    
          lblValidate.Text += myBudget.ToString() + "<br />";                

            if (numSpun == Outcome)
            {
                int newBudget = Bet * 35 + Budget;
                txtMyBudget.Text = newBudget.ToString();    
            }
            else
            {   
                int newBudget = Budget - Bet;
                txtMyBudget.Text = newBudget.ToString();    
            }    
        }        
    }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您正在更改循环中使用的myBudget而不是Budget。更改条件(使用myBudget,例如while (myBudget >= Bet))或增加Budget

顺便说一句,如果你是来自具有双向绑定的背景,那么这不是这种情况,即更改TextBox的值并不会改变{{1}的值你应该更新它的值,例如在getter中。

相关问题