hello!

well, i want to read 10 grades and put them at an array...
i want the values to be >=0 and <=10

how can i do that? what changes i have to do at my method?

using System;

class Student
{
blah 

blah

 public void SetGrades()
    {



        for (int i = 0; i <= 9; i++)
       
                Grades[i] = Double.Parse(Console.ReadLine());
           



}

static void Main()
{



blah blah

}
public SetGrades() {
    for (int i = 0; i < 10; i++) {
        do {
            Grades[i] = Double.Parse(Console.ReadLine());
            if (Grades[i] < 0 || Grades[i] > 10) {
                Console.WriteLine("Grade must be >= 0 and <= 10 but you entered {0}", Grades[i]);
            }
        } while (Grades[i] < 0 || Grades[i] > 10);
    }
}

Also, i want to prints

"give the 1st grade"
:
:
:
"give the 10th grade"


but when i write the following the code doesn't stop at "give the tenth grade"....
it's doing the loop again and again....
any ideas?

do {

Console.Write("give the 1 number");
Grades[i] = Double.Parse(Console.ReadLine());

blah blah

Console.Write("give the 10 number");
Grades[i] = Double.Parse(Console.ReadLine());

if (Grades[i] < 0 || Grades[i] > 10) {
Console.WriteLine("Grade must be >= 0 and <= 10 but you entered {0}", Grades[i]);
}
 } while (Grades[i] < 0 || Grades[i] > 10);
}
}
do {
    Console.Write("Give the {0} number", i+1);
    Grades[i] = Double.Parse(Console.ReadLine());
    if (Grades[i] < 0 || Grades[i] > 10) {
        Console.WriteLine("Grade must be >= 0 and <= 10 but you entered {0}", Grades[i]);
    }
} while (Grades[i] < 0 || Grades[i] > 10);

Line 2 of this code goes between line 3 and 4 of the previous code. That's the only change.

Edited 5 Years Ago by Momerath: n/a

This article has been dead for over six months. Start a new discussion instead.