我正在创建一个小程序,该程序接受用户输入(字符串和小数),并在用户从菜单中选择"查看摘要"选项时将其打印回给用户。
菜单根据用户输入进行循环,账户名称/账户余额将在请求/输入后立即打印,但一旦选择选项3,我就无法保存和打印。尽管有任何输入,"查看摘要"每次都显示资产为0,债务为0。
Account类有一个字符串名称和十进制余额,我只希望它保存到用户完成为止。(没有数据库。现在只是想了解概念)
我的主要问题:
这是因为循环/列表是否应该在循环之外以捕获数据
字典是个更好的主意吗?我可以从一个空的集合开始吗?
static void Main(string[] args)
{
string menuChoice;
string response;
do
{
// Welcome/Menu screen
Console.WriteLine("nWelcome to your Net Worth Calculator.n n MENU:n 1. Add Assetn 2. Add Debtn 3. View Summary");
Console.WriteLine(" ");
Console.WriteLine("What would you like to do?nPlease make a selection: ");
menuChoice = Console.ReadLine();
Account asset = new Account();
var accountList = new List<Account>();
if (menuChoice == "1")
{
do
{ //D1
Console.WriteLine("nGreat! Let's add an account.");
Console.WriteLine("Account name:");
asset.name = Console.ReadLine();
Console.WriteLine("Your account name is: " + asset.name);
Console.WriteLine("nWhat is your current account balance with " + asset.name + "?");
Console.WriteLine("Balance:");
asset.balance = Decimal.Parse(Console.ReadLine());
accountList.Add(asset);
Console.WriteLine("Your balance with " + asset.name + " is currently $" + asset.balance + ".");
Console.WriteLine("nnWould you like to add another account? y/n");
response = Console.ReadLine();
} while (response != "n");
}
if (menuChoice == "3")
{
//D3
Console.WriteLine("Let's take a look at the accounts and balances that you've added so far:");
Console.WriteLine($"{asset.balance} is your asset, and {debt.balance} is your debt.");
decimal netWorth = asset.balance - debt.balance;
Console.WriteLine(netWorth);
Console.WriteLine($"{ asset.balance}");
Console.WriteLine("nnWould you like to add another account? y/n");
response = Console.ReadLine();
}
accountList必须在输入循环之外,这样无论选择哪个选项,您都可以访问它(读或写)。
类似这样的东西:
static void Main(string[] args)
{
string menuChoice;
string response;
var accountList = new List<Account>();
// Welcome/Menu screen
Console.WriteLine("nWelcome to your Net Worth Calculator.n n MENU:n 1. Add Assetn 2. Add Debtn 3. View Summary");
do
{
Console.WriteLine(" ");
Console.WriteLine("What would you like to do?nPlease make a selection: ");
menuChoice = Console.ReadLine();
if (menuChoice == "1")
{
do
{ //D1
Account asset = new Account();
Console.WriteLine("nGreat! Let's add an account.");
Console.WriteLine("Account name:");
asset.name = Console.ReadLine();
Console.WriteLine("Your account name is: " + asset.name);
Console.WriteLine("nWhat is your current account balance with " + asset.name + "?");
Console.WriteLine("Balance:");
asset.balance = Decimal.Parse(Console.ReadLine());
accountList.Add(asset);
Console.WriteLine("Your balance with " + asset.name + " is currently $" + asset.balance + ".");
Console.WriteLine("nnWould you like to add another account? y/n");
response = Console.ReadLine();
} while (response != "n");
}
if (menuChoice == "3")
{
//D3
Console.WriteLine("Let's take a look at the accounts and balances that you've added so far:");
Console.WriteLine($"{asset.balance} is your asset, and {debt.balance} is your debt.");
decimal netWorth = asset.balance - debt.balance;
Console.WriteLine(netWorth);
Console.WriteLine($"{ asset.balance}");
Console.WriteLine("nnWould you like to add another account? y/n");
response = Console.ReadLine();
}
do { // ... Account asset = new Account(); var accountList = new List<Account>(); // ... if (menuChoice == "3") { Console.WriteLine($"{asset.balance} is your asset, and {debt.balance} is your debt."); } } while (/* ... */);
在每个菜单循环上声明一个新的assset
对象和accountList
。当然,打印时assset.balance
将为零,因为当您选择菜单选项#3时,它将始终是一个新对象。
您应该将列表移出菜单循环:
static void Main(string[] args)
{
string menuChoice;
string response;
var accountList = new List<Account>();
do { // ...
并且,在菜单选项#1:中创建Account
的新实例
if (menuChoice == "1")
{
do
{ //D1
var asset = new Account();
// ... rest of your code
} while (response != "n");
}
至于计算资产和债务,你需要找到所有分别为正数或负数的账户:
var assets = accountList.Where(x => x.balance > 0).Sum(x => x.balance);
// assuming that you used negative value for debt
var debts = accountList.Where(x => x.balance < 0).Sum(x => x.balance);
理想情况下,您需要两种不同类型的Account
来减少混乱的代码:
public abstract class Account
{
public string Name { get; set; }
public decimal Balance { get; set; }
}
public class Asset : Account { }
public class Debt : Account { }
// in option #3
accountList.OfType<Asset>().Sum(x => x.Balance);
一个问题与您所描述的完全一样,因为您在循环的每次迭代中都创建了accountList
的新实例,它会丢失上次的信息。将其移出循环,如下所示。
如果你愿意的话,我想你可以用Dictionary<string, decimal>
。优点是它不允许重复的键,并允许您按键访问值。如果你不需要这两样东西,那么List<T>
就可以了。
还有一些其他问题,所以我对你的代码采取了一些自由,但我相信下面的内容符合你的要求:
void Main()
{
string menuChoice;
string response;
var accountList = new List<Account>();
var debtList = new List<Debt>();
do
{
response = "";
Console.WriteLine("nWelcome to your Net Worth Calculator.n n MENU:n 1. Add Assetn 2. Add Debtn 3. View Summaryn 4. Quit");
Console.WriteLine(" ");
Console.WriteLine("What would you like to do?nPlease make a selection: ");
menuChoice = Console.ReadLine();
if (menuChoice == "1")
{
do
{
Account asset = new Account();
Console.WriteLine("nGreat! Let's add an account.");
Console.WriteLine("Account name:");
asset.name = Console.ReadLine();
Console.WriteLine("Your account name is: " + asset.name);
Console.WriteLine("nWhat is your current account balance with " + asset.name + "?");
Console.WriteLine("Balance:");
asset.balance = Decimal.Parse(Console.ReadLine());
accountList.Add(asset);
Console.WriteLine("Your balance with " + asset.name + " is currently $" + asset.balance + ".");
Console.WriteLine("nnWould you like to add another account? y/n");
response = Console.ReadLine();
} while (response != "n");
}
if (menuChoice == "2")
{
do
{
Debt debt = new Debt();
Console.WriteLine("nGreat! Let's add a debt.");
Console.WriteLine("Debt name:");
debt.name = Console.ReadLine();
Console.WriteLine("Your debt name is: " + debt.name);
Console.WriteLine("nWhat is your current debt balance with " + debt.name + "?");
Console.WriteLine("Balance:");
debt.balance = Decimal.Parse(Console.ReadLine());
debtList.Add(debt);
Console.WriteLine("Your balance with " + debt.name + " is currently $" + debt.balance + ".");
Console.WriteLine("nnWould you like to add another debt? y/n");
response = Console.ReadLine();
} while (response != "n");
}
if (menuChoice == "3")
{
var totalAsset = accountList.Sum(x => x.balance);
var totalDebt = debtList.Sum(x => x.balance);
Console.WriteLine("Let's take a look at the accounts and balances that you've added so far:");
Console.WriteLine($"{totalAsset} is your asset, and {totalDebt} is your debt.");
decimal netWorth = totalAsset - totalDebt;
Console.WriteLine($"Net worth: {netWorth}");
Console.WriteLine("nnPress any key to return to the menu");
Console.ReadLine();
}
} while (menuChoice != "4");
}
public class Account
{
public string name {get;set;}
public decimal balance {get;set;}
}
public class Debt
{
public string name {get;set;}
public decimal balance {get;set;}
}
摘要:
- 将
accountList
移到循环之外,这样它就不会在下一次迭代中被擦除 - 添加了
debtList
来跟踪债务(假设你有这个但没有显示?) - 为菜单选项2添加了部分(再次假设您刚刚错过了它?)
- 更改了菜单选项3中的逻辑以正确显示总计
- 添加了在选择菜单选项4(退出)时退出的逻辑
根据共同的账户/债务份额,您可能只能有一个类别,而不是两个类别。