我在这里有一个小型控制台应用程序,我希望用户输入他们的月薪,然后输入他们的“费用”,以计算出他们在扣除费用后一个月内有多少钱(用户支付所有帐单后每月有多少钱)。我想离开int Salary
。我希望支出一直增加到用户键入“ false”为止,目前bool FinishedAdding
中的变量Expenses
仅包含一个值,我想添加所有支出,然后从薪水中减去。我是正确执行此方法还是错误方法?
string NewLine = "\n";
bool Finished = false;
var Expenses = default(int);
Console.WriteLine("Enter you earn a month (after tax");
int Salary = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(NewLine);
if (Finished != true)
{
while (Finished == false)
{
Console.WriteLine("What are your expenses");
Expenses = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("You are finished?");
bool FinishedAdding = Convert.ToBoolean(Console.ReadLine());
if (FinishedAdding == true)
{
break;
}
}
}
Console.WriteLine(NewLine);
Console.WriteLine("Your total is: " + (Expenses - Salary));
答案 0 :(得分:2)
让我们逐步执行例程 。我们可以从阅读decimal
开始(它更适合Salary
之类的财务数据)
// Either enter valid decimal value or press enter (for exit)
private static bool TryReadDecimalOrExit(string title, out decimal value) {
value = 0m;
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
string input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input))
return false;
if (decimal.TryParse(input, out value))
return true;
Console.WriteLine("Sorry, invalid value. Please, try again");
}
}
private static decimal ReadDecimal(string title) {
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
string input = Console.ReadLine();
if (decimal.TryParse(input, out value))
return value;
Console.WriteLine("Sorry, invalid value. Please, try again");
}
}
循环时间:
decimal Salary = ReadDecimal("Enter you earn a month (after tax");
Decimal Expenses = 0m;
// While not exited, ask for a new expense
while (TryReadDecimalOrExit("What are your expenses", out var expense))
Expenses += expense;
Console.WriteLine("Your total is: {Salary - Expenses:c2}");
答案 1 :(得分:2)
我所做的更改:
1)最重要的是:Expenses +=
将每次输入的内容添加到先前输入的金额中。这就是您可以从薪金中扣除的总费用。
2)不用为Finished使用单独的变量,只需将Finished变量设置为是否输入“ true”或“ false”。
3)不需要if
和break
语句,只需让while
条件检查Finished
变量即可。
string NewLine = "\n";
bool Finished = false;
var Expenses = default(int);
Console.WriteLine("Enter you earn a month (after tax)");
int Salary = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(NewLine);
while (Finished == false)
{
Console.WriteLine("What are your expenses");
Expenses += Convert.ToInt32(Console.ReadLine());
Console.WriteLine("You are finished?");
Finished = Convert.ToBoolean(Console.ReadLine());
}
Console.WriteLine(NewLine);
Console.WriteLine($"Your total is: {(Salary - Expenses)}");
答案 2 :(得分:2)
使用
Expenses = Convert.ToInt32(Console.ReadLine());
您每次迭代都为Expenses
分配一个值。由于用户的支出不等于他们最后一次支出,而是总和,因此您必须对支出进行汇总
Expenses = Expenses + Convert.ToInt32(Console.ReadLine());
这可以用+=
简化,它基本上是“将一个值添加到变量的内容,并将新值分配给该变量” 。这样产生
Expenses += Convert.ToInt32(Console.ReadLine());
没有错误处理。如果我输入例如,您的程序将崩溃ei19
作为金额。尽管the answer of Dmitry提供了一种程序中错误处理的方法,但是只要键入的不是数字,它就会退出。您可能要检查输入是否有效并显示错误消息
while(!Finish)
{
var input = ReadInput("Your message");
if(ShouldExit(input))
{
Finish = true;
}
else if(IsValidAmount(input))
{
Expenses = input.Amount;
}
else
{
WriteErrorMessage("Your error message");
}
}
例如,input
的类型为UserInput
class UserInput
{
// ...
public bool Finished { get; }
public Decimal Amount { get; }
}
只是为了要点。
答案 3 :(得分:0)
def func(arr,A,B):
countA = 0
countB = 0
for i in arr:
if i in A:
countA += 1
if i in B:
countB -= 1
return countA, countB