Hi !
I have made this program , and for some reason I got a error in line 33 that says :indexOutOfRangeExeption was unhadled.
could you please, explain to me what mistake I have done in the program .
Br
Henry
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment3
{
class Program
{
static void Main(string[] args)
{
string name, message = " ";
int average = 0 ;
//Users names
Console.WriteLine("Enter you name");
name = Console.ReadLine();
// Declare variables the type array
string[] var = { "first", "second", "third" };
int[] scores = new int[3];
// loop trought the arrays
for (int i = 1 ; i <= var.Length; i ++) {
try
{
Console.WriteLine("Enter your {0}, score", var[i]);
scores[i] = int.Parse(Console.ReadLine());
average += scores[i]/3;
}
catch (FormatException) {
Console.WriteLine("the {0}, is Not a numeric value ", scores[i]);
}
}
// Testing the average
if (average >= 90 && average <= 100)
{
message = "Wow, you did great";
}
else if (average >= 80 && average <= 89)
{
message = "Pretty good!";
}
else if (average >= 75 && average <= 79)
{
message = "That was close....";
}
else if (average < 75)
{
message = "Better luck next time";
}
Console.WriteLine("Hi {0}, {1}", name, message);
Console.ReadLine();
return;
}
}
}
Loading
VulpesPosted Dec 21, 2011, 3:09 PM
The idea is that, if there's an exception, there will be another iteration of the loop.
However, if the code manages to reach the 'break' statement, then you know the input was OK and so you can safely exit the loop.
Henry LhteenmakiPosted Dec 21, 2011, 2:11 PM
thanks a lot
would like please to explain to me what while (true) means in this case ?
again thanks
VulpesPosted Dec 21, 2011, 1:49 PM
Henry LhteenmakiPosted Dec 21, 2011, 1:16 PM
instruction until the first one will correct (e.g first score )
Br
Henry
VulpesPosted Dec 21, 2011, 12:49 PM
To remedy this, just change the for statement as I've highlighted below: