2016-10-19 2 views
0

안녕하세요, 문자열과 정수를 포함하여 내 목록에 일부 데이터를 추가하려고합니다. 학생의 이름, 성 및 전화 번호를 같은 목록에 저장하여 수업을 사용하고 싶습니다. 여기 내 코드는C에서 목록에 여러 데이터 추가 #

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 


namespace educationsystem 
{ 
    public class student 
     { 
      public string name { get;set;} 
      public string lastname {get;set;} 
      public long phone {get;set;} 
     } 
      List<student> Students = new List<student>(); 
      Students.Add(new student {name= "mahta",lastname= "sahabi",phone= "3244"}); 
class Program 
{ 

    static void Main(string[] args) 
    { 


     Console.WriteLine("please choose one of the numbers below : "+"\n"); 
     Console.WriteLine("1.adding new student"+"\n"); 
     Console.WriteLine("2.adding new course"+"\n"); 
     Console.WriteLine("3.adding new grade"+"\n"); 
     Console.WriteLine("4.showing the best student"+"\n"); 
     Console.WriteLine("5.getting students average"+"\n"); 
     Console.WriteLine("6.exit"+"\n"); 
     string input = Console.ReadLine(); 


      if (input == "1") 
      { 
       Console.Clear(); 
       List<int> grades = new List<int>(); 

        Console.WriteLine("please enter the students name"); 
        string name = Console.ReadLine(); 
        Console.WriteLine("please enter the students last name"); 
        string lastname = Console.ReadLine(); 
        Console.WriteLine("please enter the students phone number"); 
        long phone = Convert.ToInt32(Console.ReadLine()); 
        Console.WriteLine(name +" "+ lastname +" " + phone); 





      } 
      else if (input == "2") 
      { 

      } 
      else if (input == "3") 
      { 

      } 
      else if (input == "4") 
      { 

      } 
      else if (input == "5") 
      { 

      } 


     Console.ReadKey(); 
} 

} 
     } 

입니다. 그러나 작동하지 않습니다. 내 목록 이름은 학생이지만 시스템에서 인식하지 못합니다. 제발 도와 주실 래요?

+0

를 만들 수있는 PHP와 C++

class Program { public List<student> Students = new List<student>(); } 

같은 언어로 할 수 있는가? –

답변

2

클래스 내에 목록을 넣어야합니다. C#에서는 변수를 전역으로 만들 수 없습니다. 당신이 당신의 클래스 학생들이 글로벌 accesible 확인하려면 전역 변수는 학생 또는 성적을 나열 그것은 정적

public static List<student> Students = new List<student>(); 
+0

정말 도움이 된 덕분 –

+0

내 대답으로 문제가 해결되면 큰 체크 박스를 클릭하여 대답으로 받아들입니다. –

+0

출력물에 내 목록을 어떻게 인쇄해야합니까? –