2015-06-19 114 views
0

我想输入学生的标识和标记到数组中。问题陈述是,如果用户输入学生的身份证,那么应该显示该学生的马克。以下是我的代码到目前为止。你可以帮我吗?从数组中检索数据

int [] mark = new int [5] ; 
string [] studentsid = new string [5]; 
string userInput = ""; 
bool found = false; 
int i = 0;  

string[] answer = new string[5]; 
for (i = 0; i < answer.Length; i++) 
{ 
    Console.WriteLine("Enter Student " + (i + 1) + " 's ID Number: "); 
    studentsid[i] = Console.ReadLine(); 
    Console.WriteLine("Enter student" + (i + 1) + "'s mark: "); 
    mark[i] = Convert.ToInt32(Console.ReadLine()); 
} 
Console.WriteLine("Enter one of you student's id number"); 

userInput = Console.ReadLine(); 
if (studentsid[i].ToUpper() == userInput.ToUpper()) 
{ 
    found = true; 
    Console.WriteLine(mark[i]);       
} 

if (mark[i] >=85 && mark[i] <= 100) 
{ 
    Console.WriteLine("Distinction"); 
} 
Console.ReadKey(); 
+1

你遇到的实际问题是什么? –

+0

这是一个很好的*开始*的问题,但特别是当你正在学习编码时,你需要更好地识别实际问题。请参阅http://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question – BradleyDotNET

+1

你正在代码中你想要的部分没有循环找到正确的ID,其余的代码看起来不错(ish)。 – fvu

回答

4

您需要在代码块周围放置第二个循环来检查学生的ID是否匹配。

现在,您只是检查一个学生(数组中的最后一名学生)是否与用户的输入相匹配。你也在循环之外使用了for循环的控制变量,这通常被认为是不好的做法。

考虑这样的事情:

Console.WriteLine("Enter one of you student's id number"); 

userInput = Console.ReadLine(); 
for (int i = 0; i < studentsid.length; i++) 
{ 
    if (studentsid[i].ToUpper() == userInput.ToUpper()) 
    { 
     found = true; 
     Console.WriteLine(mark[i]);       
    } 
} 

而且,你的 “答案” 数组没有任何用处。您只能创建它来检查其硬编码的长度为5.使用studentsid的长度代替。

最后,两个数组并不是真正存储这种类型数据的理想方式。 使用学生ID作为键和标记作为值的地图将是存储和访问此数据的更有效的方法。

+0

伟大的答案,因为它包括修复+一些建设性的反馈。 – 40Alpha