C#程序存储多个学生记录,名称为1年级 - 5年级,平均成绩

时间:2018-05-19 14:19:28

标签: c# arrays class

我正在尝试创建一个程序,根据用户输入的学生数量,然后记住提示5个年级和每个学生的平均值。

学生记录的一个例子如下:

  

学生#......等级1 ......等级2 ......等级3 ......等级4 ......等级5 ......平均

到目前为止

代码(无论如何可能都不正确):

Vue.use(window.vuelidate.default)
const { required, minValue } = window.validators

new Vue({
  el: "#app",
  data: {
    text: ''
  },
  validations: {
    text: {
        minValue: minValue(moment(new Date(), 'DD.MM.YYYY').format('DD.MM.YYYY')),
    }
  }
})

我不知道从哪里开始,因为在将名称存储到数组中时出错。

2 个答案:

答案 0 :(得分:0)

您无法将字符串“Student 1”解析为双倍,如下面的代码所示:

   string studentId = "Student 1"; //error here
   students[0].addGrade(double.Parse(studentId)); 

我不知道你想要的结果是什么。但请确保学生ID是一个数字值(可以是字符串形式)。您可以使用以下代码来获得所需内容。

        Console.WriteLine("Input the number of students: ");
        int numberOfStudents = int.Parse(Console.ReadLine());

        Student student = new Student();

        string studentId = "1";
        student.addGrade(double.Parse(studentId));

        double value;
        value = double.Parse(Console.ReadLine());
        student.addGrade(value);

        value = double.Parse(Console.ReadLine());
        student.addGrade(value);

        value = double.Parse(Console.ReadLine());
        student.addGrade(value);

        value = double.Parse(Console.ReadLine());
        student.addGrade(value);

        value = double.Parse(Console.ReadLine());
        student.addGrade(value);

        double avarageNumber = student.getAverage();

答案 1 :(得分:0)

使用new创建数组不会创建每个单独的元素(int之类的基元除外)。您需要为数组中的每个项目调用new

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Input the number of students: ");
        int n = int.Parse(Console.ReadLine()); 

        // Create array of size n (instead of 6)
        // Array is created but individual Students are not created yet!
        Student[] students = new Student[n];

        // Use a loop to get all student info
        for (int s = 0; s < n; s++) {
            string studentId = $"Student{s+1}";

            // Important! Need to create a student object
            students[s] = new Student(studentId);

            // Use another loop to get grades
            for (int g = 0; g < 5; g++) {
                double grade = double.Parse(Console.ReadLine());
                students[s].addGrade(grade);
            }

            // Print average
            Console.WriteLine($"{students[s].id} average = {students[s].getAverage()}");
        }
    }

另外,修改Student课程以获得id

    public class Student
    {
        private ArrayList grades;
        public string id;

        // Accepts one parameter : an id
        public Student(string studentId)
        {
            id = studentId;
            grades = new ArrayList();
        }

        public void addGrade(double val)
        {
            grades.Add(val);
        }

        public double getAverage()
        {
            double avg = 0;
            for (int i = 0; i < grades.Count; i++)
            {
                avg += (double)grades[i];
            }
            avg /= grades.Count;
            return avg;
        }
    }
}