i am very new to programming.i am stuck on adding a for loop to my program.this is the requirements:Design and write a
program to read names and exam marks of 8 students and output names and
equivalent exam grades in each case. Grades are to be worked out as
follows: if the mark is <40 then grade is a FAIL; for a mark in the range
40-59, the grade is PASS; if the mark is 60-79 then it is MERIT and for a mark
>=80 the grade is DISTINCTION. Your program should accept a numeric value in
the range 0-100. For any other value, the program should display an error
message and terminate. Use a switch
statement for selection and a for loop for repetition. This is what i have so far after the Main method:
{
int marks;
string name;
Console.WriteLine("Enter name");
name = Console.ReadLine();
Console.WriteLine("Enter marks");
marks = int.Parse(Console.ReadLine());
if (marks < 40)
{
Console.WriteLine("Fail");
}
else if (marks >= 40 && marks <= 59)
{
Console.WriteLine("Pass");
}
if (marks >=60 && marks <=79)
{
Console.WriteLine("Credit");
}
else if (marks >= 80 && marks <= 100)
{
Console.WriteLine("Distinction");
}
if(marks <0 || marks >100)
{
Console.WriteLine("Data error" );
}
Loading
AlanPosted Oct 28, 2008, 6:45 PM
Try it like this:
static void Main()
{
int marks;
string name;
for(int student = 1; student <= 8; student++)
{
Console.WriteLine();
Console.WriteLine("Enter details for student " + student);
Console.WriteLine();
Console.Write(" Name : ");
name = Console.ReadLine();
Console.Write(" Marks : ");
marks = int.Parse(Console.ReadLine());
if (marks < 0 || marks > 100)
{
Console.WriteLine();
Console.WriteLine("Error : marks must be between 0 and 100");
return;
}
switch(marks/20)
{
case 2:
Console.WriteLine(" Grade : Pass");
break;
case 3:
Console.WriteLine(" Grade : Merit");
break;
case 4:
case 5:
Console.WriteLine(" Grade : Distinction");
break;
default:
Console.WriteLine(" Grade : Fail");
break;
}
}
Console.WriteLine();
Console.Write("Press enter to end program");
Console.ReadLine();
}
mariaPosted Oct 29, 2008, 12:58 PM