简体   繁体   中英

Create average grade with double areas

I must create a console application. Write a LepsiStudent method that performs the following parameters on your input: the name of the first student field of first student marks the name of the second student field of second student marks The method calculates the arithmetic means of the marks of these students and lists which student has a better average.

In the main method, prepare two fields of marks and student names. Then call the LepsiStudent method.

I code this and I don't know how to continue average grade can someone help me please?

{
    class Program
    {
        static void Main(string[] args)
        {
            double[] znamkyjan = { 2, 4 };
            double[] znamkydan = { 1, 5 };
            LepsiStudent(znamkyjan,znamkydan);
        }
        static void LepsiStudent (double [] znamky, double[] znamky2)
        {
            Console.WriteLine("Jan Novák");
            foreach (var znam in znamky)
            {
                //Console.WriteLine(znam);
                Console.WriteLine(znam / 2);
            }
            Console.WriteLine("Daniel Havlík");
            foreach (var znam2 in znamky2)
            {
                Console.WriteLine(znam2);
            }
        }
    }
}

To calculate an average you need to sum the values and then divide by the number of grades, and you want to do that for each student. We could do this with less variables, but this is a little more readable for someone new to programming. we go over all the first student grades- sum them and divide, and the same for 2nd student- then we compare and print the result..

    static void LepsiStudent(double[] znamky, double[] znamky2)
    {
        double student1Sum = 0;
        double student1Avg = 0;
        double student2Sum = 0;
        double student2Avg = 0;

        foreach (var znam in znamky)
        {
            student1Sum += znam;
        }
        student1Avg = student1Sum / znamky.Length;

        foreach (var znam2 in znamky2)
        {
            student2Sum += znam2;
        }
        student2Avg = student2Sum / znamky2.Length;

        if (student1Avg > student2Avg)
        {
            Console.WriteLine("first student has the higher average");
        }
        else if (student1Avg == student2Avg)
        {
            Console.WriteLine("neither student has the higher average");
        }
        else
        {
            Console.WriteLine("second student has the higher average");
        }
    }

Just for fun, this does the same thing with linq:

static void LepsiStudent(double[] znamky, double[] znamky2)
{
    double student1Avg = znamky.Sum() / znamky.Length; ;
    double student2Avg = znamky2.Sum() / znamky2.Length;
    string name = (student1Avg == student2Avg) ? "neither" : 
                  Math.Max(student1Avg, student2Avg) == student1Avg ? "first" : "second";
    Console.WriteLine($"{name} student has the higher average");
}

The Linq library has a built-in Average method:

using System;
using System.Linq;
                    
public class Program
{
        public static void Main(string[] args)
        {
            double[] znamkyjan = { 2, 4, 5, 7 };
            double[] znamkydan = { 1, 5, 5, 9 };
            LepsiStudent(znamkyjan,znamkydan);
        }
        static void LepsiStudent (double [] znamky, double[] znamky2)
        {
            double m1 = znamky.Average();
            double m2 = znamky2.Average();
            Console.WriteLine("Jan Novák: " + m1);
            Console.WriteLine("Daniel Havlík: " + m2);          
        }
}

Output

Jan Novák: 4.5
Daniel Havlík: 5

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