读入整数数组,输入字符串时停止
Reading in an integer array, and stopping when a string is entered
int [] arr = new int [100];
int count = 0;
int val;
arr[count] = 0;
while(count < arr.Length && (int.TryParse(arr[count], out val)))
{
arr[count] = Convert.ToInt32.Console.ReadLine();
}
我希望用户输入数组的值,当他们输入时 'exit' while 循环中断
您应该首先使用 do..while
循环读取输入,并在用户输入 exit
时使用退出条件退出 while 循环
do
{
string input = Console.ReadLine(); //Read input from user
if(input == "exit") //exit condition
break;
if(int.TryParse(input, out int intData))
arr[count++] = intData;
}while(count < arr.Length);
int [] arr = new int [100];
int count = 0;
int val;
arr[count] = 0;
while(count < arr.Length && (int.TryParse(arr[count], out val)))
{
arr[count] = Convert.ToInt32.Console.ReadLine();
}
我希望用户输入数组的值,当他们输入时 'exit' while 循环中断
您应该首先使用 do..while
循环读取输入,并在用户输入 exit
do
{
string input = Console.ReadLine(); //Read input from user
if(input == "exit") //exit condition
break;
if(int.TryParse(input, out int intData))
arr[count++] = intData;
}while(count < arr.Length);