2017-02-27 36 views
-2

我做了一类学生有一个参数课程和成绩,这是数组,但当我尝试添加一个学生,它不允许我输入数组中的细节我只面包车只输入一个词或数字我不能添加信息作为一个数组在我的班学生

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

我想你可能想阅读约占阵列如何工作。请参阅:https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx – aquinas

+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}); 
+0

当然,等级也是一个数组,我需要新的int [] {50} – aquinas

+0

@aquinas:哦,我明白了,谢谢! –

+0

非常感谢你现在的工作 –

相关问题