2016-01-29 50 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。虽然,我会等这两个选项,直到你更好地理解第一个和第二个解决方案。

相关问题