Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#程序存储多个学生记录,包括姓名、1年级至5年级和平均成绩_C#_Arrays_Class - Fatal编程技术网

C#程序存储多个学生记录,包括姓名、1年级至5年级和平均成绩

C#程序存储多个学生记录,包括姓名、1年级至5年级和平均成绩,c#,arrays,class,C#,Arrays,Class,我正在尝试创建一个程序,该程序使用用户输入的学生数量,然后记住提示5个年级和每个学生的平均成绩 学生记录的示例如下: 学生。。。。。。一年级。。。。。二级。。。。。三年级。。。。。四年级。。。。。五年级。。。。。平均值 到目前为止的代码(无论如何可能都不正确): 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 名称空间问题二 { 班级计划 { 静态void M

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

学生记录的示例如下:

学生。。。。。。一年级。。。。。二级。。。。。三年级。。。。。四年级。。。。。五年级。。。。。平均值

到目前为止的代码(无论如何可能都不正确):

使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间问题二
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“输入学生人数:”);
int n=int.Parse(Console.ReadLine());
学生[]学生=新生[6];
字符串studentId=“Student 1”;//此处出错
students[0].addGrade(double.Parse(studentId));//或此处出错
双重价值;
value=double.Parse(Console.ReadLine());
学生[1]。增加分数(价值);
value=double.Parse(Console.ReadLine());
学生[2]。增加分数(价值);
value=double.Parse(Console.ReadLine());
学生[3]。增加分数(价值);
value=double.Parse(Console.ReadLine());
学生[4]。增加分数(价值);
value=double.Parse(Console.ReadLine());
学生[5]。增加分数(价值);
学生[6].getAverage();
}
公立班学生
{
私人审裁官职系;
公立学生()
{
等级=新的ArrayList();
}
公共坡率(双val)
{
添加(val);
}
公共双平均值()
{
双平均值=0;
对于(int i=0;i

我不确定该从这里开始,因为我在将名称存储到数组中时出错。

您无法将字符串“Student 1”解析为double,如以下代码所示:

   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();

使用
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[]Student=newstudent[6]
你只需要
Student=newstudent()并将
学生[0]
等替换为
学生
。这将是一个学生的成绩和一个学生的平均成绩。为6个学生做这件事,你需要更多的代码更改。现在你将得到NullReferenceException。是的,我得到了。
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()}");
        }
    }
    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;
        }
    }
}