1

对于我的期末考试,我必须用 C# 编写一个程序。假设是一个银行账户,用户可以从原始账户余额中添加或提取金额。该程序本身可以运行,但由于某种原因,它确实第二次使用更新后的余额。相反,它仍然使用期初余额。例如,您一开始有 100 美元,取出 20 美元,新余额为 80 美元。下次您添加甚至移除资金时,它会从 100 美元而不是 80 美元中扣除。关于我可能做错了什么的任何想法?

到目前为止,这是我的代码:

namespace BankingAccount
{
    public partial class Entrance : Form
    {
        public string name;
        public int number;
        public double total;
        public double with;
        public double dep;

        public Entrance()
        {
            InitializeComponent();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            setVisPart1(true);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtAcctName.Clear();
            txtAcctName.Enabled = true;
            txtAcctNumber.Clear();
            txtAcctNumber.Enabled = true;
            lblBalance.Text = "Beginning Balance:";
            txtBalance.Clear();
            txtBalance.Enabled = true;
            setVisibility(false);
        }

        private void btnContinue_Click(object sender, EventArgs e)
        {
            string line;
            int val;
            double value;

            name = txtAcctName.Text;

            line = txtAcctNumber.Text;

            if (int.TryParse(line, out val))
            {
                if (val < 0)
                {
                    MessageBox.Show("Invalid number");

                    txtAcctNumber.Clear();
                }
                else
                {
                    number = int.Parse(txtAcctNumber.Text);

                    txtAcctNumber.Enabled = false;
                }
            }
            else
            {
                MessageBox.Show("Invalid number");

                txtAcctNumber.Clear();
            }

            line = txtBalance.Text;

            if (double.TryParse(line, out value))
            {
                total = double.Parse(txtBalance.Text);

                txtBalance.Enabled = false;
                lblBalance.Text = "Available Balance:";
            }
            else
            {
                MessageBox.Show("Invalid number");

                txtBalance.Clear();
            }

            txtAcctName.Enabled = false;
            setVisPart2(true);
            btnContinue.Visible = false;
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            string line;
            double value;

            total = double.Parse(txtBalance.Text);

            line = txtWithdrawal.Text;

            if (double.TryParse(line, out value))
            {
                with = double.Parse(txtWithdrawal.Text);
            }
            else
            {
                MessageBox.Show("Invalid number");

                txtWithdrawal.Clear();
            }

            line = txtDeposit.Text;

            if (double.TryParse(line, out value))
            {
                dep = double.Parse(txtDeposit.Text);
            }
            else
            {
                MessageBox.Show("Invalid number");

                txtDeposit.Clear();
            }

            Banking anAccount = new Banking(name, number, total, with, dep);

            txtBalance.Text = anAccount.Balance.ToString("C");

            txtWithdrawal.Clear();
            txtDeposit.Clear();
        }

        private void setVisibility(bool visibilityValue)
        {
            lblName.Visible = visibilityValue;
            lblNumber.Visible = visibilityValue;
            lblBalance.Visible = visibilityValue;
            lblWithdrawal.Visible = visibilityValue;
            lblDeposit.Visible = visibilityValue;
            txtAcctName.Visible = visibilityValue;
            txtAcctNumber.Visible = visibilityValue;
            txtBalance.Visible = visibilityValue;
            txtWithdrawal.Visible = visibilityValue;
            txtDeposit.Visible = visibilityValue;
            btnCalculate.Visible = visibilityValue;
            btnContinue.Visible = visibilityValue;
        }

        private void setVisPart1(bool visibilityValue)
        {
            lblName.Visible = visibilityValue;
            lblNumber.Visible = visibilityValue;
            lblBalance.Visible = visibilityValue;
            txtAcctName.Visible = visibilityValue;
            txtAcctNumber.Visible = visibilityValue;
            txtBalance.Visible = visibilityValue;
            btnContinue.Visible = visibilityValue;
        }

        private void setVisPart2(bool visibilityValue)
        {
            lblWithdrawal.Visible = visibilityValue;
            lblDeposit.Visible = visibilityValue;
            txtWithdrawal.Visible = visibilityValue;
            txtDeposit.Visible = visibilityValue;
            btnCalculate.Visible = visibilityValue;
        }
    }
}

这是我的类编码:

namespace BankingAccount
{
    class Banking
    {
        private string acctName;
        private int acctNumber;
        private double balance;
        private double withdrawal;
        private double deposit;

        public Banking(string name, int number, double total, double with, double dep)
        {
            acctName = name;
            acctNumber = number;
            balance = total;
            withdrawal = with;
            deposit = dep;
            DetermineBalance();
        }

        public string AcctName
        {
            set
            {
                acctName = value;
            }
            get
            {
                return acctName;
            }
        }

        public int AcctNumber
        {
            set
            {
                acctNumber = value;
            }
            get
            {
                return acctNumber;
            }
        }

        public double Withdraw
        {
            set
            {
                withdrawal = value;
            }
            get
            {
                return withdrawal;
            }
        }

        public double Deposit
        {
            set
            {
                deposit = value;
            }
            get
            {
                return deposit;
            }
        }

        public double Balance
        {
            set
            {
                balance = value;
            }
            get
            {
                return balance;
            }
        }

        public void DetermineBalance()
        {
            balance = balance + (deposit - withdrawal);
        }
    }
}
4

2 回答 2

0

我认为问题在于 ToString("C") 在解析字符串时将其加倍,它有一些无法正确解析的歌声。

于 2013-01-07T03:11:12.723 回答
0

由于这都是内存中的(也就是说..没有使用数据库),这都是范围的问题。

范围定义了哪些变量在哪里可见。Banking每次在按钮单击中计算时,您都在定义一个对象。一旦按钮单击方法完成,该对象将不可用。

要补救,请在类级别声明它:

public partial class Entrance : Form
{
    public string name;
    public int number;
    public double total;
    public double with;
    public double dep;

    public Banking anAccount = new Banking(); // <---- declare it up here

我将把它作为“读者的任务”来讨论如何让它发挥作用。就目前而言,我的回答对您没有帮助,因为您的构造函数需要参数。考虑删除该构造函数并使用对象的公共属性。每次点击计算,而不是重新创建Banking对象.. 设置属性并调用CalculateBalance.

(我很抱歉没有给你一个直截了当的答案。但由于这显然是家庭作业,我在道德上有义务不只是给你答案。也许其他人是?我给你的虽然很多!)

于 2013-01-07T04:44:35.530 回答