如何一起添加用户输入

时间:2013-05-06 19:55:14

标签: c#

我的程序必须将之前输入的数字添加到下一个。这就是我到目前为止所做的,而且我遇到了困难。它累计了我输入的数字,但我不明白我如何验证之前的一个数字。

static void Main(string[] args)
{
    const int QUIT = -1;
    string inputStr;
    int inputInt = 0;
    do
    {
        Console.Write("Type a number (type -1 to quit): ");
        inputStr = Console.ReadLine();
        bool inputBool = int.TryParse(inputStr, out inputInt);

        if (inputBool == true)
            inputInt += inputInt;

        Console.WriteLine("Sum of the past three numbers is: {0}", inputInt);
    } while (inputInt != QUIT);
}

由于可能有无限数量的条目,我不知道如何正确使用数组。

5 个答案:

答案 0 :(得分:2)

如果您正在尝试查找数字总和,请使用另一个变量。

static void Main(string[] args)
{
    const int QUIT = -1;
    string inputStr;
    int inputInt = 0,tempint=0;
    do
    {
        Console.Write("Type a number (type -1 to quit): ");
        inputStr = Console.ReadLine();
        bool inputBool = int.TryParse(inputStr, out tempint);

        if (inputBool == true)
        {
            inputInt += tempint;
        }

        Console.WriteLine("Sum of the past three numbers is: {0}", inputInt);

    } while (tempint!= QUIT);


}

答案 1 :(得分:0)

如果您想要最简单的解决方案,您可以跟踪过去的3个数字并在打印时总结它们

static void Main(string[] args)
{
    const int QUIT = -1;
    string inputStr;
    int i1 = 0;
    int i2 = 0;
    int i3 = 0;
    int inputInt = 0;
    do
    {
        Console.Write("Type a number (type -1 to quit): ");
        inputStr = Console.ReadLine();
        bool inputBool = int.TryParse(inputStr, out inputInt);

        if (inputBool == true)
        {
            i3 = i2;
            i2 = i1;
            i1 = inputInt;
        }

        Console.WriteLine("Sum of the past three numbers is: {0}", i1+i2+i3);

    } while (inputInt != QUIT);


}

答案 2 :(得分:0)

这个答案使用LINQ和队列来做你想做的事。

static void Main(string[] args)
{
   const int QUIT = -1;
   string inputStr;
   int inputInt = 0;

   Queue myQ = new Queue();

   do
   {
      Console.Write("Type a number (type -1 to quit): ");
      inputStr = Console.ReadLine();
      bool inputBool = int.TryParse(inputStr, out inputInt);

      if (inputBool == true)
      {
         if (myQ.Count() == 3)
         {
            myQ.Dequeue();
            myQ.Enqueue(inputInt);
         }
         else
         {
            myQ.Enqueue(inputInt);
         } 
      }

      if (myQ.Count() == 3)
      {
         Console.WriteLine("Sum of the past three numbers is: {0}", myQ.Sum());
      }

   } while (inputInt != QUIT);

}

答案 3 :(得分:0)

使用列表存储所有数字。然后在结尾计算列表中有多少项,Sum()整个列表

static void Main(string[] args)
{
    const int QUIT = -1;
    string inputStr;
    List<int> allNumbers = new List<int>();
    do
    {
        Console.Write("Type a number (type -1 to quit): ");
        inputStr = Console.ReadLine();
        bool inputBool = int.TryParse(inputStr, out inputInt);

        if (inputBool == true)
            allNumbers.Add(inputInt); // Add a new element to the list
        Console.WriteLine("Sum of the past " + allNumbers.Count + " numbers is: {0}", allNumbers.Sum());
    } while (inputInt != QUIT);
}

答案 4 :(得分:0)

您需要创建一个局部变量来保存int.TryParse的输出。此外,您不需要执行while循环,当输入 -1 时,您可以立即退出。通过这些更改,跟上最后3个数字变得更加容易:

static void Main(string[] args)
{
    const int QUIT = -1;
    int[] last3 = new int[3];
    // infinite loop, exit is done when input is -1
    for(;;) {
        Console.Write("Type a number (type -1 to quit): ");
        var input = Console.ReadLine();
        int tmp; // holds the int value of the input
        if (int.TryParse(input, out tmp))
        {
            if (tmp == QUIT)
                break; // input is -1

            // input was an int, so lets move the last two towards
            // the front of the array, last3[0] contained the oldest value
            // which is gone now
            last3[0] = last3[1];
            last3[1] = last3[2];

            // now overwrite the last item in the array with the newest value 
            last3[2] = tmp;
        }

        // add up the values, note if input was not a valid int, this will sum
        // the last 3 valid values because the above if statement will only execute
        // and change the values in the array if the user input was a number
        var sum = last3[0] + last3[1] + last3[2];

        Console.WriteLine("Sum of the past three numbers is: {0}", sum);
    }
}