我制作了一个学生班级,其中包含参数课程和成绩,但是当我尝试添加学生时,它不允许我输入详细信息,因为在数组中我只输入一个单词或数字
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);
}
}
}
答案 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} );