Когда я пытаюсь ввести строку выхода, ххх, почему выдается исключение? Он работает в цикле while , но не в цикле do ... while .
После преобразования в целое число строковая переменная позволяет использовать только числовые символы (например, -999) для выхода из цикла do ... while . Но я хочу сделать переменную управления циклом словом вроде «выход», а не цифрами. Как я могу это сделать?
Вот мой код.
using System;
namespace StringInputPractice
{
class StringInputPractice
{
static void Main()
{
// Declarations
string inValue;
int first;
int second;
int sum;
do
{
Console.Write("\nEnter the first number. (Type \"xxx\" to exit): ");
inValue = Console.ReadLine();
first = Convert.ToInt32(inValue); //@@@@@
Console.Write("\nEnter the second number. (Type \"xxx\" to exit): ");
inValue = Console.ReadLine();
second = int.Parse(inValue);
sum = first + second;
Console.WriteLine("\nThe sum of {0} and {1} is {2}.", first,
second, sum);
/* Things I've tried inside do { } and that don't work */
//inValue = "";
//inValue = null;
//inValue = inValue.ToString();
//inValue = first.ToString();
//inValue = second.ToString();
}
while (inValue != "xxx"); /*If you enter a non-numeric string,
* an exception is thrown at
* @@@@@ above.
*/
Console.ReadLine();
}
}
}