class Program
{
static void Main(string[] args)
{
int[] salaryRange = { 300, 400, 500, 600, 700, 800, 900, 1000};
int[] frequency = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
bool doContinue = true;
while(doContinue)
{
Console.WriteLine("Please enter salary");
string resultStr = Console.ReadLine();
int result;
bool canParse = int.TryParse(resultStr, out result);
if (canParse)
{
if (result < 200)
{
Console.WriteLine("Wrong input - salary cannot be less than 200");
continue; //ask for input again
}
else
{
for(int i =0;i<frequency.Length;i++){
if (i==8)
{
frequency[i] = frequency[i] + 1;
}
else if (result < salaryRange[i])
{
frequency[i] = frequency[i] + 1;
break;
}
}
}
}
else
{
Console.WriteLine("Wrong input - should be numeric");
continue; //ask for input again
}
Console.WriteLine("Do you want to continue? Y/N");
resultStr = Console.ReadLine();
if (!(resultStr.ToLower() == "y"))
{
doContinue = false;
}
}// end while
// print results
Console.WriteLine("\n\n===============\nFrequency");
for (int i = 0; i< frequency.Length ; i++)
{
string lower;
string upper;
if (i == 0)
{
lower = "200";
}
else
{
lower = "" + (salaryRange[i-1]);
}
if (i == 8)
{
upper = "over";
}
else
{
upper = "" + (salaryRange[i]-1);
}
Console.WriteLine(lower + " - " + upper + ": \t" + frequency[i]);
}
Console.ReadLine();
}
}
|