标签云

微信群

扫码加入我们

WeChat QR Code

I'm writing a console application that can be used by teachers to save and retrieve data about a student.I want to retrieve elements from the lists of studentInfoName and studentInfoClassAndNumber - so that the user can enter the name of the student and get all the other informations, like middle name, last name, class and number.I tried using a foreach loop but it doesn't seem to work.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication2{class Program{static void Main(string[] args){DrawStarLine();DrawTitle();DrawMenu();int answer = int.Parse(Console.ReadLine());if (answer == 1){Console.WriteLine("Enter information about a student");List<string> studentInfoName = new List<string>();Console.Write("Name: ");studentInfoName.Add(Console.ReadLine());Console.Write("Middle name: ");studentInfoName.Add(Console.ReadLine());Console.Write("Last name: ");studentInfoName.Add(Console.ReadLine());List<int> studentInfoClassAndNumber = new List<int>();Console.Write("Class ");studentInfoClassAndNumber.Add(int.Parse(Console.ReadLine()));Console.Write("Number ");studentInfoClassAndNumber.Add(int.Parse(Console.ReadLine()));}else if (answer == 2){Console.WriteLine("Enter the name of the student that you want to retrieve information about");?????????????????????????????????????????????????}else{ErrorMessage();}Console.ReadKey();}private static void ErrorMessage(){Console.WriteLine("Typing error, press key to continue.");}private static void DrawStarLine(){Console.WriteLine("************************");}private static void DrawTitle(){DrawStarLine();Console.WriteLine("+++ Student Information Database +++");DrawStarLine();}private static void DrawMenu(){DrawStarLine();Console.WriteLine(" 1. Enter information about a student.");Console.WriteLine(" 2. Retrieve information about a student available in the program");Console.WriteLine(" 3. Exit from the program");DrawStarLine();Console.WriteLine("Make your choice: type 1, 2, or 3 for exit");DrawStarLine();}}}


Where is the list you want to retrieve elements from? What do you want to achieve?

2019年04月19日33分50秒

I want to retrieve elements from the lists studentInfoName and studentInfoClassAndNumber.

2019年04月19日33分50秒

I want the user to enter the name of the student and get all the other information, like middle name, last name, class and number :)

2019年04月19日33分50秒

Added further explanation from comments and edited for readability

2019年04月18日33分50秒

Error1A local variable named 'studentName' cannot be declared in this scope because it would give a different meaning to 'studentName', which is already used in a 'parent or current' scope to denote something else

2019年04月19日33分50秒

Error2A local variable named 'studentInfoClassAndNumber' cannot be declared in this scope because it would give a different meaning to 'studentInfoClassAndNumber', which is already used in a 'parent or current' scope to denote something else

2019年04月19日33分50秒

i get those errors when i try to put the variables.

2019年04月18日33分50秒

Did you make sure to remove the variable declarations from your if statement? you only need to declare them one time. If you wanted you can still reinitialize them just don't use the type i.e. variableName = new... not Type variableName = new...

2019年04月18日33分50秒

I seem to understand your answer better than the others. Thank you. I will do that and I will see how to useDictionary> to support multiple students. I will write again. :)

2019年04月18日33分50秒