2014-09-28 47 views
-1

我正在用C#編寫一個程序,控制檯應用程序,我遇到了一個問題。如何在不同的方法中使用變量?

到目前爲止,我已經有五類的:主類,這是Program和其他四個TeacherStudentCourseClass

我的問題是,我收到來自用戶的信息和信息都在另一種方法。

我希望能夠使用我從用戶處獲得的輸入,並以不同的方法使用它來編輯輸入。

只是說程序應該能夠編輯輸入的,我不能使它的工作?!? ,這是我寫的代碼:

struct Students 
{ 
    public string name; 
    public string family; 
    public int ID; 
    public string Major; 
    public int studentCode; 
} 

class Student 
{ 
    public static void ShowStudent() 
    { 
     Console.Clear(); 
     Console.WriteLine("This is Student's Section:\n"); 
     Console.WriteLine("===================================="); 
     Console.WriteLine("Enter the Number Of the Section you want to Work with:\n"); 
     Console.WriteLine("1 Submit"); 
     Console.WriteLine("2 Edit"); 
     Console.WriteLine("3 Back"); 
     string choice = Console.ReadLine(); 

     switch (choice) 
     { 
      case "1": 
       ReciveStudent(); 
       break; 
      case "2": 
       break; 
      case "3": 
       Program.mainShow(); 
       break; 
      default: 
       Console.Clear(); 
       Console.WriteLine("Please Enter INrange Number.\nPress any Key to Continue...."); 
       Console.ReadKey(); 
       ShowStudent(); 
       break; 
     } 
    } 

    public static void ReceiveStudent() 
    { 
     Console.Clear(); 
     int n; 

     Console.WriteLine("How Many Student's you Want to Enter?"); 
     n = Convert.ToInt32(Console.ReadLine()); 
     Students[] st = new Students[n]; 

     for (int i = 0; i < n; i++) 
     { 
      Console.WriteLine("Enter Student ID:"); 
      st[i].ID = Convert.ToInt32(Console.ReadLine()); 
      Console.WriteLine("Enter Student CODE:"); 
      st[i].studentCode = Convert.ToInt32(Console.ReadLine()); 
      Console.WriteLine("Enter Student Name:"); 
      st[i].name = Console.ReadLine(); 
      Console.WriteLine("Enter Student Family:"); 
      st[i].family = Console.ReadLine(); 
      Console.WriteLine("Enter Student Major:"); 
      st[i].Major = Console.ReadLine();       
     } 

     ShowStudent(); 
    } 
} 

ShowStudent()方法被稱爲主,它是program.csvoid main)。

我想用不同的方法使用數組值。

感謝您的評論...

謝謝你們。

+0

你有沒有考慮從'Receive'方法返回'st'陣列和存儲以備將來使用的主要方法裏面? – 2014-09-28 15:12:41

+0

@EugenePodskal你確定它有效嗎? 因爲我認爲當接收完成後,該值將被更改爲空。 – 2014-09-28 15:29:55

回答

1

返回方法RecieveStudent從用戶收集的數據,並將該數據用作另一個方法的輸入。

更改RecieveStudent到:public static Students[] RecieveStudent()

public static Students[] RecieveStudent() { 
    // your code here as is 
    return st; 
} 

,並使用返回數組作爲輸入到其他方法:

Students[] students = ReceiveStudent(); 
otherMethod(students) // Do other work on data 

順便說一句,您的命名可以使用一些工作。例如。你的結構學生實際上並沒有模擬一羣學生,但只有一個。

編輯:

struct Students 
{ 
    public string name; 
    public string family; 
    public int ID; 
    public string Major; 
    public int studentCode; 
} 

class Student 
{ 
    public static void ShowStudent() 
    { 
     Console.Clear(); 
     Console.WriteLine("This is Student's Section:\n"); 
     Console.WriteLine("===================================="); 
     Console.WriteLine("Enter the Number Of the Section you want to Work with:\n"); 
     Console.WriteLine("1 Submit"); 
     Console.WriteLine("2 Edit"); 
     Console.WriteLine("3 Back"); 
     string choice = Console.ReadLine(); 
     Students[] students = null; 

     switch (choice) 
     { 
      case "1": 
       students = ReciveStudent(); 
       break; 
      case "2": 
       break; 
      case "3": 
       Program.mainShow(); 
       break; 
      default: 
       Console.Clear(); 
       Console.WriteLine("Please Enter INrange Number.\nPress any Key to Continue...."); 
       Console.ReadKey(); 
       ShowStudent(); 
       break; 
     } 

     // Use variable 'students' here 
     // Remember to check for null as it might not have been mutated in the switch-case. 
     if (students != null) 
     { 
      // Do something with students here... just printing it for now. 
      Console.WriteLine(students.ToString()); 
     } 
    } 

    public static Students[] ReceiveStudent() 
    { 
     Console.Clear(); 
     int n; 

     Console.WriteLine("How Many Student's you Want to Enter?"); 
     n = Convert.ToInt32(Console.ReadLine()); 
     Students[] st = new Students[n]; 

     for (int i = 0; i < n; i++) 
     { 
      Console.WriteLine("Enter Student ID:"); 
      st[i].ID = Convert.ToInt32(Console.ReadLine()); 
      Console.WriteLine("Enter Student CODE:"); 
      st[i].studentCode = Convert.ToInt32(Console.ReadLine()); 
      Console.WriteLine("Enter Student Name:"); 
      st[i].name = Console.ReadLine(); 
      Console.WriteLine("Enter Student Family:"); 
      st[i].family = Console.ReadLine(); 
      Console.WriteLine("Enter Student Major:"); 
      st[i].Major = Console.ReadLine();       
     } 

     ShowStudent(); 

     return st; 
    } 
} 
+0

謝謝你的解決方案... 我剛開始這個,你可以說它是原型模式。 – 2014-09-28 15:19:49

+0

Np :)考慮接受它,如果它回答你的問題 – ssnielsen 2014-09-28 15:22:08

+0

雖然靜態類數組可以工作,但這是一種不好的做法。再次,正確的解決方案將包括一個控制檯應用程序,該控制檯應用程序可以跟蹤一個StudentCollection,一個Student實例可以顯示自己的Student類以及一個可以輸入新學生或編輯現有實例的StudentEditor對象。但顯然,這是過度的。 – Pieter21 2014-09-28 15:23:37

相關問題