如何在while循环中添加具有相同变量的循环变量(如果可能)

时间:2020-03-11 12:08:20

标签: c# while-loop

我在这里有一个小型控制台应用程序,我希望用户输入他们的月薪,然后输入他们的“费用”,以计算出他们在扣除费用后一个月内有多少钱(用户支付所有帐单后每月有多少钱)。我想离开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));

4 个答案:

答案 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)不需要ifbreak语句,只需让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