1

我的程序必须将输入的前一个数字添加到下一个数字中。这就是我到目前为止所拥有的,我被卡住了。它将我输入的数字加起来,但我不明白如何验证以前的数字。

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);
}

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

4

5 回答 5

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);


}
于 2013-05-06T20:03:29.180 回答
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);


}
于 2013-05-06T19:58:09.900 回答
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);

}
于 2013-05-06T20:08:27.603 回答
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);
}
于 2013-05-06T20:12:55.273 回答
0

您需要创建一个局部变量来保存来自int.TryParse. 此外,您不需要 do 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);
    }
}
于 2013-05-06T20:19:53.143 回答