2017-02-27 38 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

非常感謝你現在的工作 –

相關問題