2016-01-29 47 views
1

我是C#編程的初學者。我對這個簡單的程序感到震驚,我想顯示符合條件的候選人。我的問題是,如果知道他/她符合條件,我該如何儲存候選人的姓名。如何在C中存儲帶有函數的字符串#

int eligble = 0; //For counting the eligble Number of candidates 

    bool retry;   //For trying until the Number of eligble candidates is reached 
    retry = true; 
    while (retry) 
    { 
     string candidatename; //Intilization for Candidate Name ,Date of Birth ,10th and 12th Percentages 
     int tper, twper; 
     string dob; 


     Console.WriteLine("Please enter your Name"); //Getting user input values 
     candidatename = Console.ReadLine(); 
     Console.WriteLine("Please enter your date of birth in dd/mm/yyyy format"); 
     dob = Console.ReadLine(); 
     DateTime dt = Convert.ToDateTime(dob); 
     Console.WriteLine("Please enter your 12th percentange"); 
     twper = Convert.ToInt16(Console.ReadLine()); 
     Console.WriteLine("Please enter your 10th percentange"); 
     tper = Convert.ToInt16(Console.ReadLine()); 

     int age1 = age(dt); 
     if (eligble > 5)  //Checking whether we have selected the Eligble amount of candidates 
     { 
      Console.WriteLine("We have selected five eligble candidtes"); 
      retry = false; 
      Console.WriteLine("n"); 
     } 
     else 
     { 
      if (age1 > 20 && twper > 65 && tper > 60) //Checking Whether the candidate have satisfiyed the Conditions 
      { 
       eligble += 1; 
       string grad = rade(twper, tper); 
      } 
      else 
      { 
       eligble -= 1; 
      } 
     } 
    } 
} 

static int age(DateTime _dt)         //Function for calculating the age of the candidate 
{ 
    DateTime n = DateTime.Now;         // To avoid a race condition around midnight 
    int age = n.Year - _dt.Year; 

    if (n.Month < _dt.Month || (n.Month == _dt.Month && n.Day < _dt.Day)) 
     age--; 

    return age; 
} 

static string rade(int _tper, int _twper) 
{ 
    string grade1; 
    int avg= (_tper+_twper)/ 2; 
    if (avg > 90) 
    { 
     grade1 = "a"; 
     return grade1; 
    } 
    else if (avg > 80 && avg < 80) 
    { 
     grade1 = "b"; 
     return grade1; 
    } 
    else if (avg > 70 && avg < 79) 
    { 
     grade1 = "c"; 
     return grade1; 
    } 
    else 
    { 
     grade1 ="d"; 
     return grade1; 
    } 
} 
+1

你想如何「存儲」?在一個文本文件? – Ian

+0

你的代碼示例的頂部缺失 – PiotrWolkowski

+0

No ..我想要動態地存儲它並顯示它@Ian – vikram

回答

1

作出新的List對象,這樣做的事:

List<string> eligableCandidates = new List<string>(); 

,然後當你想要的東西添加到列表中做到:

eligableCandidates.Add(candidateName); 

希望這有助於

Jason。

2

「商店」具有廣泛的含義。您的程序運行時可以將數據存儲在內存中。在那種情況下,C#提供了大量的collections。如果你只是想保留它們,列表將工作。

var names = new List<string>(); 
names.Add(candidate.Name); 

如果您願意將它們存儲與某種關鍵的,然後使用該密鑰來從收集的價值更好的選擇將是一個字典:

var myEligibleCandidates = new Dictionary<string, string>(); 
myEligibleCandidates[candidate.Id] = candidate.Name; 

這些選項將保留應用程序運行時的值。 如果你希望你的值也可以在程序沒有運行後存儲,你可以使用一個文件來完成。靜態File類可以是一個良好的開端:

public void WriteToFile(string path, List<string> names) 
{ 
    File.WriteAllText(path, string.Join(";", names)); 
} 

這種方法將名作爲參數列表,並會寫他們用分號分隔的文件。 path是文件的路徑。

然後可以選擇將數據保存到數據庫中。如果這是你想要做的,那麼看看Entity FrameworkADO.NET。雖然,我會等這兩個選項,直到你更好地理解第一個和第二個解決方案。

相關問題