嘿,我之前参加过上一堂课的Java时涉猎OOP。我正在用C#制作银行帐户表格。我每次花一点时间,以便我可以从我的错误和错误中学习。我有我的程序文件和我的帐户文件。我评论了错误所在的位置以及它们在我的程序文件中所说的内容,并将其张贴在下面。谢谢您抽出宝贵的时间来帮助我
更新这是我的更新代码:
命名空间BankAccountBank { 公共局部类Form1:表单 { 公共Form1() { InitializeComponent(); }
private static void Bank(string[] args)
{
// Create a list of accounts
var accounts = new List<Account>
{
new Account("Dopey", 500), //just put these accounts as a place holder
new Account("Sleepy"),
new Account("Sneezy", 300)
};
// Write these records to a file
WriteFile(accounts); //I still don't see a text file after running this
}
private static void WriteFile(List<Account> accounts)
{
throw new NotImplementedException();
}
private void Submitbtn_Click(object sender, EventArgs e)
{
double x;
if (OpenRdo.Checked == true && Nametxtbx.TextLength > 0)
{
double.TryParse(Nametxtbx.Text, out x);
MessageBox.Show("Account Created", "Create Account", MessageBoxButtons.OK, MessageBoxIcon.Information);
Nametxtbx.Text = Name; // should it be acctName?
Random acct = new Random();
int AccountNumber = acct.Next(5, 1000);// after running and clicking the button there no number it says system.random
outputlbl.Text = acct.ToString();
}
void WriteFile(List<Account> accts) //"static is not valid for this item" what other item can i put here then?
{
StreamWriter outputFile = File.CreateText("accounts.txt");
string record;
foreach (var acct in accts)
{
record = $"{acct.Name},{acct.Balance}";
Console.WriteLine($"Writing record: {record}");
outputFile.WriteLine(record);
}
outputFile.Close();
}
}
帐户文件:
namespace BankAccountBank
{
class Account
{
public string Name { get; }
public decimal Balance { get; private set; }
public Account(string acctName, decimal acctBalance = 0)
{
Name = acctName;
Balance = acctBalance;
}
public void Deposit(decimal amt)
{
Balance += amt;
}
public void Withdraw(decimal amt)
{
if (Balance - amt >= 0)
Balance -= amt;
else
throw new ArgumentOutOfRangeException("Can't withdraw more than balance");
}
override public string ToString()
{
return $"Account: {Name} / Balance = {Balance:C}";
}
}
}
答案 0 :(得分:0)
Main
负责成为程序的入口点。特别是,它显示第一个表单。它实际上不应该是Form1
的一部分。将该方法命名为其他名称。
如果您需要该函数在启动时运行,则可以从Form1
的构造函数或Loaded
事件的处理程序中调用它。
WriteFile
完全被弄乱了,因为它是在另一种方法中定义的。。方法不应该这样做(从C#7技术上来说,您可以)(匿名或“ lambda”方法除外)。将其放入类范围,就可以了
static
成员。好的经验法则是,在您真的知道自己在做什么之前,仅避免使用static
(需要Main
除外)。