繁体   English   中英

循环没有正确地将数据存储在数组中 C#

[英]Loop is not storing data in array correctly C#

嘿伙计们,我目前正在为一个班级项目制定银行计划。 如果用户尚未创建帐户,则需要创建一个帐户,但如果他们已经创建,则只需使用帐号和密码登录即可。 然而。 而不是我的程序不断向大小为 100 的数组添加数据,它只是替换插槽 [0] 中的数据,只是想知道为什么。

public partial class Form1 : MetroForm
{
    //For Creating new account
    string newAccountType;
    Accounts[] customers = new Accounts[99999];
    int temp;
    string VerifyPin = ("");
    private void openAccount_Click(object sender, EventArgs e)
    {

        for (int index=0;index < customers.Length; ++index)
        {
            var R1 = new Random();
            var R2 = new Random();



            customers[index] = new Accounts();
            customers[index].Name = newName.Text;
            customers[index].accountType = newAccountType;
            customers[index].accountNumber = (R1.Next(1000000,9000000))+(R2.Next(100,9000));
            customers[index].accountPin = createPin.Text;
            customers[index].accountBalance = 100.00;
            temp = index;


        }

        MetroMessageBox.Show(this, "Thank you member "+customers[temp].Name+"\nYour member number is: "+customers[temp].accountNumber, "You are now a memeber", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
        metroTabControl1.SelectedTab = metroTabPage2;

    }

        private void checkBalance_Click(object sender, EventArgs e)
        {
        int veri=0;
        bool isfound = false;
            for (int count = 0; count < customers.Length; ++count)
                {
            if (Convert.ToInt32(userName.Text) == customers[count].accountNumber)
            {
                veri = count;
                isfound = true;

            }
            else
                isfound = false;
            accountnotfound.Text = "Account Not Found";


        }
        if (isfound && (customers[veri].accountPin == pinText.Text))
        {
            MetroMessageBox.Show(this, "account found", "account found");
        }
        else
        {
            MetroMessageBox.Show(this, "account not found or wrong pin", "account not found");
            pinText.Text = "";
        }

        accountBalance.Visible = true;
        userWithdraw.Visible = true;
        userDeposite.Visible = true;
        accountBalance.Text = "Welcome, "+customers[veri].Name+"\nYour current balance is: "+customers[veri].accountBalance;


    }

public class Accounts
{
    private string name, AccountType, AccountPin;
    private int AccountNumber;
    private double AccountBalance;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    public int accountNumber
    {
        get
        {
            return AccountNumber;
        }
        set
        {
            AccountNumber = value;
        }
    }
    public string accountPin
    {
        get
        {
            return AccountPin;
        }
        set
        {
            AccountPin = value;
        }
    }
    public string accountType
    {
        get
        {
            return AccountType;
        }
        set
        {
            AccountType = value;
        }
    }
    public double accountBalance
    {
        get
        {
            return AccountBalance;
        }
        set
        {
            AccountBalance = value;
        }
    }
}
for (int index=0;index < customers.Length; ++index)

customers[index] = new Accounts();

每次数组循环时,索引将为 0,因此每次调用 openAccount_Click 时都会替换 customers[0]。

你真的不需要循环添加。 您只需要确保您正在编写下一个可能的索引。 也许使用您的 Temp 变量并为每个添加的用户增加它 + 1,然后没有循环就这样做。

customers[Temp] = new Accounts();
customers[Temp].Name = newName.Text;
customers[Temp].accountType = newAccountType;
customers[Temp].accountNumber = (R1.Next(1000000,9000000))+   (R2.Next(100,9000));
customers[Temp].accountPin = createPin.Text;
customers[Temp].accountBalance = 100.00;
Temp += 1;

您最好为您的全球客户使用帐户列表。

List<Accounts> customers = new List<Accounts>();

然后在 openAccount_Click 中,您可以像这样添加一个新帐户。

customers.Add(new Accounts {
Name = newName.Text, 
accountType = newAccountType,
accountNumber = (R1.Next(1000000,9000000))+(R2.Next(100,9000)),
accountPin = createPin.Text,
accountBalance = 100.00
});

我认为您只需要跟踪数组中的哪个元素可用(即具有空值)。 一旦数组中没有元素为空,这意味着客户数组已满,因此此时您无法添加更多客户。

进行以下更改。 在代码开始循环的部分代码中:

bool added = false;

for (int index=0;index < customers.Length; ++index)
{
    if (customers[index] != null) continue;
    ...

此外,在您实际添加客户后,请执行以下操作(在循环结束后立即执行):

if (!added)
{
    // show error message here!
}
else
{
    MetroMessageBox.Show(this, "Thank you member "+customers[temp].Name+"\nYour member number is: "+customers[temp].accountNumber, "You are now a memeber",     MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
    metroTabControl1.SelectedTab = metroTabPage2;
}

布尔添加 = 假; for (int x = 0; x <= temp; x++)

        {

            if (customers[temp] != null) continue;
                {
                for (int indexies = 0; indexies < customers.Length; indexies++)
                {



                        var R1 = new Random();
                        var R2 = new Random();
                        Convert.ToInt32(createPin.Text);


                        customers[temp] = new Accounts();
                        customers[temp].Name = newName.Text;
                        customers[temp].accountType = newAccountType;
                    customers[temp].accountNumber = 1;//(R1.Next(1000000, 9000000)) + (R2.Next(100, 9000));
                        customers[temp].accountPin = Convert.ToInt32(createPin.Text);
                        customers[temp].accountBalance = 100.00;
                        added = true;


                }


                }


            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM