C# 我能';t在我的班级中添加信息作为数组
我制作了一个学生类,其中有一个参数“课程和成绩”是数组,但当我尝试添加一个学生时,它不允许我输入详细信息,因为在数组中,我只能输入一个单词或数字C# 我能';t在我的班级中添加信息作为数组,c#,C#,我制作了一个学生类,其中有一个参数“课程和成绩”是数组,但当我尝试添加一个学生时,它不允许我输入详细信息,因为在数组中,我只能输入一个单词或数字 Student s1 = new Student(dev, 1691676, "eng,maths", 50 ); 这是密码 public class Student { public string name; public int studentId; public string[] courses; pu
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);
}
}
}
公共班级学生
{
公共字符串名称;
公共int学生;
公共课程;;
公共职等;;
公立学生(字符串名称、int studentId、字符串[]课程、int[]成绩)
{
this.name=名称;
this.studentId=studentId;
本课程=课程;
这个。等级=等级;
}
公开资料
{
Console.WriteLine(“学生姓名为”+姓名);
Console.WriteLine(“学生的StudentID为”+StudentID);
Console.WriteLine(“学生所修课程为”+课程);
Console.WriteLine(“学生获得的分数为”+分数);
}
公众睡眠
{
Console.WriteLine(“输入学生睡眠的时间”);
int sleep=Convert.ToInt32(Console.ReadLine());
INTA=等级。长度;
int b=0;
如果(b您必须添加一个数组,string[]
,而不是单个string
;但是您可以将字符串拆分为数组:
// 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} );
Grades当然也是一个数组,因此您需要新的int[]{50}我想您可能想了解数组的工作原理。请参阅:非常感谢。它确实很有用:)