简体   繁体   English

创建双区域的平均成绩

[英]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.编写一个 LepsiStudent 方法,对您的输入执行以下参数: 第一个学生的第一个学生字段的名称标记 第二个学生标记的第二个学生字段的名称 该方法计算这些学生的分数的算术平均值并列出哪些学生有更好的平均分。

In the main method, prepare two fields of marks and student names.在main方法中,准备marks和学生姓名两个字段。 Then call the LepsiStudent method.然后调用 LepsiStudent 方法。

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:只是为了好玩,这与 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: Linq库有一个内置的Average方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算学生的平均绩点(GPA) - How to calculate the grade point average(GPA) of students C#程序,用于存储具有名称,1年级-5年级和平均水平的多个学生记录 - C# Program to store multiple student record with name, grade1 - grade5, and average 计算双数组行的平均值 - Calculate the average of an double array rows 如何使用for循环从文本框中的单选按钮值计算和显示平均成绩? - How to calculate and display average grade from radio button values in a textbox using a for loop? 如何通过文本文件在控制台上显示学生的姓名和平均成绩? - How can I display the student's name and his average grade on the console from a text file? 我正在尝试创建一个字母等级计算器,但一直收到错误 - I'm trying to create a letter grade calculator but keep receiving errors 如何阻止我的平均输出显示与原始成绩输出相同的字母成绩? 另外如何使哨兵值不区分大小写? - How can I stop my average output from displaying the same letter grade as my original grade output? Also how to make sentinel values case insensitve? LINQ计算SortedList的移动平均值<dateTime,double> - LINQ to calculate a moving average of a SortedList<dateTime,double> 创建日期和平均值查询 - create a query for date and average values 从位图的非透明区域创建Region或GraphicsPath - Create a Region or GraphicsPath from the non-transparent areas of a bitmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM