简体   繁体   中英

C# Beginner Program Pass an Array to a Method

New c# learner, this program is to input grades and then figure the average. However, I want to use specific student ID numbers (ie 3345, 6654, etc.) rather than the looped numbers as IDs. How can I edit this code to reflect this? Thanks in advance.

 class ClassScores
    {
        private Score[] scores;
        private int index;

        public ClassScores(int n)
        {
            scores = new Score[n];
            index = 0;
        }

        public void addScore(Score obj)
        {
            scores[index] = obj;
            index++;
        }

        public Score highScore()
        {
            Score obj = scores[0];
            for (int i = 0; i < index; i++)
            {
                if (scores[i].getGrade() > obj.getGrade())
                {
                    obj = scores[i];
                }
            }

            return obj;
        }

        public double average()
        {
            int sum = 0;
            for (int i = 0; i < index; i++)
            {
                sum += scores[i].getGrade();
            }

            return (sum / (double)(scores.Length));
        }
    }
}
 class Score
    {
        private int id;
        private int grade;

        public Score(int studentID, int studentGrade)
        {
            id = studentID;
            grade = studentGrade;
        }

        public void setId(int studentID)
        {
            id = studentID;
        }

        public void setGrade(int score)
        {
            grade = score;
        }

        public int getGrade()
        {
            return grade;
        }

        public int getId()
        {
            return id;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            ClassScores classOne = new ClassScores(6);


            for (int i = 1; i <= 6; i++)
            {
                Console.Write("Enter Score for Student " + i + ": ");
                int grade = Convert.ToInt32(Console.ReadLine());


                Score classTwo = new Score(i, grade);


                classOne.addScore(classTwo);
            }

            Console.WriteLine("\n\n Average Score: " + classOne.average());

            Score high = classOne.highScore();

            Console.WriteLine("\n\n Student with id: " + high.getId() + " has a Highest Score: " + high.getGrade() + "\n\n");

            Console.Read();
        }
    }

It will need an array of six different student ID numbers.

You can change your for loop to prompt the user to enter the Student ID . You can then pass that to the Score class constructor:

for (int i = 1; i <= 6; i++)
{
    Console.Write("Enter Student ID:");
    int id = Convert.ToInt32(Console.ReadLine());

    Console.Write("Enter Score for Student " + id + ": ");
    int grade = Convert.ToInt32(Console.ReadLine());

    Score classTwo = new Score(id, grade);

    classOne.addScore(classTwo);
}

EDIT

If you already have your Student ID's in an array you can loop over that array:

int[] studentIds = new[] { 3524, 4321, 546, 7896, 4327, 123 };

ClassScores classOne = new ClassScores(studentIds.Length);

foreach(int id in studentIds)
{
    Console.Write("Enter Score for Student " + id + ": ");
    int grade = Convert.ToInt32(Console.ReadLine());

    Score classTwo = new Score(id, grade);

    classOne.addScore(classTwo);
}

We have changed the for loop to a foreach which will loop over each item in the array and retrieve each value

If you still want to use a for loop instead then you can use the loop variable to get the ID from the array - remember arrays are zero based so you need to loop from zero to the length of the array:

for (int i = 0; i < studentIds.LEngth; i++)
{
    int id = studentIds[i];
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM