While loop and keyboard reading
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
namespace nsLoops
{
using System;
public class Loops
{
static public void Main ()
{
int [] arr = new int [5];
for (int x = 0; x < arr.Length; ++x)
{
bool done = false;
while (!done)
{
Console.Write ("Enter a value for element {0}: ", x);
string str = Console.ReadLine ();
int val;
try
{
val = int.Parse (str);
}
catch (FormatException)
{
Console.WriteLine ("Please enter an integer value\r\n");
continue;
}
arr[x] = val;
done = true;
}
}
int index = 0;
foreach (int val in arr)
{
Console.WriteLine ("arr[{0}] = {1}", index, val);
++index;
}
}
}
}
Related examples in the same category