【发布时间】:2016-01-29 02:08:45
【问题描述】:
我是 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;
}
}
【问题讨论】:
-
你想如何“存储”?在文本文件中?
-
缺少代码示例的顶部
-
不..我想动态存储并显示它@Ian
-
@vikram 在这种情况下,您可以考虑使用
List,最重要的是,考虑创建您的Candidateclass或struct,以防万一您想存储Candidate的多个元素,而不仅仅是一个string。在这种情况下,List<string>是不够的。但是List<Candidate>会。 :) -
以上
if-else是检查合格候选人的正确方法吗??
标签: c# storing-data