【问题标题】:Bank Account Program Deposit Button is not working银行账户程序存款按钮不起作用
【发布时间】:2012-04-05 11:31:23
【问题描述】:

该程序是一个有两个选项卡的 GUI。在第一个选项卡上有四个文本框,分别代表姓名、身份证、年龄和帐户余额。此选项卡上还有一个按钮,可将帐户添加到第二个选项卡上的组合框中。在第二个选项卡上,有名称、id、年龄和余额的组合框和四个文本框。当我从组合框中选择一个名称时,四个文本框会自动填写其信息。我遇到的问题是我必须有一个提款和一个存款按钮,用户可以输入一个金额并将其减去或添加到文本框中的余额中。我有一些撤回按钮的示例代码,有人已经帮我解决了。谁能告诉我为什么当我点击按钮时它没有改变平衡?

class BankAccount
{
    //attributes
    public string accountID;
    public string customerName;
    public int customerAge;
    public double balance;
    public const double DEFAULT_BALANCE = 500.00;

    //construct
    public BankAccount()
    {
    }

    public BankAccount(string anID, string aName, int anAge, double aBalance)
    {
        accountID = anID;
        customerName = aName;
        customerAge = anAge;
        balance = aBalance;
        if (aBalance == 0)
        {
            balance = DEFAULT_BALANCE;
        }
        else
        {
            balance = aBalance;
        }
    }

    public BankAccount(string anID, string aName, int anAge)
    {
        accountID = anID;
        customerName = aName;
        customerAge = anAge;
        balance = DEFAULT_BALANCE;
    }






    //accessors
    public void SetID(string anID)
    {
        accountID = anID;
    }

    public void SetName(string aName)
    {
        customerName = aName;
    }

    public void SetAge(int anAge)
    {
        customerAge = anAge;
    }

    public void SetBalance(double aBalance)
    {
        balance = aBalance;
    }

    public string GetID()
    {
        return accountID;
    }

    public string GetName()
    {
        return customerName;
    }

    public int GetAge()
    {
        return customerAge;
    }

    public double GetBalance()
    {
        return balance;
    }


}

}

这是表格

 public partial class Form1 : Form
 {

    private List<BankAccount> account = new List<BankAccount>();

    public Form1()
    {
        InitializeComponent();
    }



    private void btnAddAccount_Click(object sender, EventArgs e)
    {
        BankAccount aBankAccount = new BankAccount(txtAccountID.Text, txtName.Text,
            int.Parse(txtAge.Text), double.Parse(txtBalance.Text));

        account.Add(aBankAccount);
        AddToComboBox();
        ClearText();


    }

    private void AddToComboBox()
    {
        cboAccount.Items.Clear();
        foreach (BankAccount person in account)
        {

            cboAccount.Items.Add(person.GetName());


        }


    }
    private void ClearText()
    {
        txtName.Clear();
        txtAccountID.Clear();
        txtBalance.Clear();
        txtAge.Clear();
        txtAccountID.Focus();


    }

    private void cboAccount_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtNameTab2.Text = account[cboAccount.SelectedIndex].customerName;
        txtAgeTab2.Text = account[cboAccount.SelectedIndex].customerAge.ToString();
        txtAccountIDTab2.Text = account[cboAccount.SelectedIndex].accountID.ToString();
        txtBalanceTab2.Text = account[cboAccount.SelectedIndex].balance.ToString();
    }



    private void btnWithdraw_Click(object sender, EventArgs e)
    {

        double amount = 0;

        if (double.TryParse(txtWithdraw.Text, out amount))
        {
            if (amount > 0)
            {
                BankAccount currentAccount = account[cboAccount.SelectedIndex];
                double currentBalance = currentAccount.GetBalance();
                double amountLeft = currentBalance - amount;

                if (amountLeft >= 0)
                {
                    currentAccount.SetBalance(amountLeft);
                    txtBalanceTab2.Text = amountLeft.ToString("c");
                }
                else
                {
                    MessageBox.Show("You don't have enough money!");
                }



            }

        }
    }


}
}

【问题讨论】:

  • 这里有相当多的代码。当您通过它进行调试时,观察到的行为在什么时候偏离了预期的行为?确定那条线,指出值是什么,并解释你期望它做什么。
  • 这也不仅仅是家庭作业的味道;是吗?
  • 这并不能解决您的问题,但是您为什么要在 BankAccount 类中使用单独的 GetSet 方法。属性可能更合适
  • 它运行得很好。它没有任何问题。当我点击撤回按钮时,什么也没有发生。
  • @psubsee2003:看起来像 Java 开发人员正在转向 .NET。

标签: c# webforms


【解决方案1】:

“谁能告诉我为什么当我按下按钮时它没有改变平衡?”

撤回按钮有效,因为有一个事件编码来处理点击:

private void btnWithdraw_Click(object sender, EventArgs e)

余额按钮没有任何此类事件。

我推荐一本好书作为参考,在谷歌上搜索一些编程知识并请你的朋友帮助指导你可能很难。编码是艺术、科学、数学和技术的融合,而且很复杂。

【讨论】:

  • 我目前拥有的书籍似乎没有多大帮助
  • @jamesclemens 是的,这就是为什么提到请您的朋友作为导师提供帮助的原因。当我开始时,这对我来说完全没有意义。诸如类是对象、委托等之类的东西毫无意义。我有一位年长的朋友用简单的英语向我解释了我正在编写的示例。
【解决方案2】:

您的按钮中有两个 if 语句。其中一个或两个都可能是错误的。在 if 语句附近放置断点并查看调试时传递的值。或者,如果 Amountleft 不大于 0,则将 else 语句与您使用的消息框类似。您应该得到一些东西。

如果它有助于为所有值设置断点以查看正在传递的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2016-06-06
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多