我无法在班级学生中添加信息作为数组

时间:2017-02-27 20:34:23

标签: c#

我制作了一个学生班级,其中包含参数课程和成绩,但是当我尝试添加学生时,它不允许我输入详细信息,因为在数组中我只输入一个单词或数字

Student s1 = new Student(dev, 1691676, "eng,maths", 50 ); 

这是代码

  public class Student
  {
    public string name;
    public int studentId;
    public string[] courses;
    public int[] grades ;


    public Student(string name,int studentId,string[] courses,int[] grades)
    {
        this.name = name;
        this.studentId = studentId;
        this.courses = courses;
        this.grades = grades;

    }
    public void info() 
    {
        Console.WriteLine("name of the student is" + name);
        Console.WriteLine("StudentID of the student is" + studentId);
        Console.WriteLine("courses taken by student are" + courses);
        Console.WriteLine("grades earned by the student are" + grades);

    }
    public void sleep() 
    {
        Console.WriteLine("enter the amount of time the student slept");
        int sleep=Convert.ToInt32(Console.ReadLine());
        int a = grades.Length;
        int b = 0;
        if (b<a)
        {
            grades[b] = grades[b] - (sleep / 10);
        }       
    }
  }

1 个答案:

答案 0 :(得分:2)

您必须添加数组string[],而不是string;但你可以Split串入一个数组:

// As aquinas noticed, grades is an array as well as courses
Student s1 = new Student(dev, 1691676, "eng,maths".Split(','), new int[] {50} );

或按预期提供数组:

Student s1 = new Student(dev, 1691676, new string[] {"eng", "maths"}, new int[] {50} );