2013-05-06 41 views
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

使用[表](http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx) – Axis 2013-05-06 19:58:06

+0

这是不清楚你想要做什么;如果你有“无限数量的条目”,你是否试图将所有这些数字相加?最后三个?每组三个? – 2013-05-06 20:08:00

回答

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


} 
+0

谢谢,这工作! – 2013-05-06 20:20:50

+0

那个文字'“过去三个数字的总和是:”'虽然让我感到不安。如果你不输入三个数字,那不是,是吗?这不是由于这个答案的问题;这是原始问题中的文本看起来不对。 – 2013-05-06 20:28:27

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


} 
+0

使用列表将是一个更好的做法。 – dotTutorials 2013-05-06 20:01:49

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

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