我的程序没有使用新更新的total,而是使用原始的total

本文关键字:total 原始 程序 新更新 我的 更新 | 更新日期: 2023-09-27 17:50:59

期末考试,我必须用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);
        }
    }
}

我的程序没有使用新更新的total,而是使用原始的total

由于这是所有内存(即…没有使用数据库),这都是作用域的问题。

Scope定义了什么变量在…每次在按钮单击中计算时,都定义了一个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

我很抱歉没有给你一个直截了当的回答。但因为这显然是家庭作业,我有道德义务不直接给你们答案…也许是别人?我已经给了你很多了!)

我认为问题在于ToString("C")在解析字符串的时刻翻倍它有一些唱歌不能正确解析