我们如何在列表中的控制台中显示输入的详细信息 [英] How can we display entered details in console in a list
问题描述
我在编辑时输入了控制台中的员工详细信息,现在我需要在alist中显示这些详细信息。
我尝试过:
namespace Program1
{
class Employee
{
public string Empname =;
public int EMPID = 0;
// public void print()
// {
// Console.WriteLine(\ n员工ID为+ EMPID);
// Console.WriteLine(\ n员工姓名是+ Empname);
//}
//声明员工ID
public int EmployeeID
{
get;
设定;
}
//声明员工姓名
公共字符串EmployeeName
{
get;
设定;
}
//公共覆盖字符串ToString()
// {
// for(int i = 0; i< 24; i ++)//只需用户静态数组长度
// {
// EMPID [i] .Number = Console.ReadLine();
// Empname [i] .Name = Console.ReadLine();
//}
//返回EmployeeID =+ EMPID +,EmployeeName =+ Empname;
//}
// foreach(newlist.OrderBy中的var项目(Employee => Employee.empid))Console.WriteLine(Employee);
}
class程序
{
public static void Main()
{
// Employee e = new Employee(); //创建对象employee
// //e.EmployeeID = 501;
// //e.EmployeeName =Anusha;
// //Console.WriteLine(员工数据:{0},e.EmployeeID);
// //Console.WriteLine(员工数据:{0},e.EmployeeName);
// //Console.ReadLine();
// for(int i = 0; i< 2; i ++)
// {
// Console.WriteLine(输入员工ID,e.EmployeeID);
// Console.ReadLine();
// Console.WriteLine(输入员工姓名,e.EmployeeName);
// Console.ReadLine();
// string line = Console.ReadLine();
// if(line ==exit)
// {
// break;
//}
//}
//Console.WriteLine(\"Employee ID:+ e.EMPID +'\t'+员工姓名:+ e.Empname) ;
//Console.ReadLine();
Console.WriteLine(输入员工ID);
var EmpID = int.Parse(Console.ReadLine());
var employee1 = new Employee
{
EmployeeID = EmpID;
}
}
}
//公共类预订
// {
//公共字符串BookName {get;组; } = string.Empty;
//公共字符串作者{get;组; } = string.Empty;
//公共字符串ISBN {get;组; } = string.Empty;
//}
}
您的问题在这里:
< pre lang =c#> public static void Main()
{
Employee e = new Employee(); // 使用员工创建对象
( int i = 0 ; i< 2; i ++)
{
Console.WriteLine( 输入员工ID,e .EmployeeID);
Console.ReadLine();
Console.WriteLine( 输入员工姓名,e.EmployeeName);
Console.ReadLine();
string line = Console.ReadLine();
if (line == exit )
{
break ;
}
}
Console.WriteLine( 员工ID: + e.EMPID + ' \t' + 员工姓名: + e.Empname);
Console.ReadLine();
}你只创建了一个Employee实例 - 所以你可以在你的循环中做所有事情(如果你的代码做了什么,它现在甚至都不会编译,更不用说了做任何有用的事情) - 覆盖你已经拥有的内容。
它不会编译,因为你的EMployee类包含的代码不在方法内:
class 员工
{
public string Empname = ;
public int EMPID = 0 ;
public int EmployeeID
{
获得跨度>;
set ;
}
public string EmployeeName
{
< span class =code-keyword> get ;
set ;
}
Console.WriteLine(Employee); <<< ----此行不是' t in method
}
但你真的需要回到基础并重新开始...
class 员工
{
public int EmployeeID { get ; set ; }
public string EmployeeName { get 跨度>; set ; }
}将为您的Employee类做。
现在,使用它:
public static void Main()
{
列表<员工> employees = new 列表< Employee>();
for ( int i = 0 ; i< 2; i ++)
{
员工员工= 新员工();
Console.WriteLine( 输入员工姓名,e.EmployeeName);
string line = Console.ReadLine();
if (line == exit )
{
break ;
}
...填写 此处的员工详细信息
employees.Add(employee);
}
foreach (员工员工 员工)
{
...打印员工详细信息
}
Console.ReadLine();
}
您需要创建一个列表,并在每个循环期间将创建的Employee对象添加到该列表中。然后在最后你可以使用你创建的列表。
public static void Main()
{
List< Employee> employees = new 列表< Employee>();
for ( int i = 0 ; i< 2; i ++)
{
员工e = 新员工(); // 使用员工创建对象
Console.WriteLine( 输入员工ID:);
e.EmployeeID = int .Parse(Console.ReadLine());
Console.WriteLine( 输入员工姓名:);
e.EmployeeName = Console.ReadLine();
employees.Add(e);
string line = Console.ReadLine();
if (line == exit )
{
break ;
}
}
foreach (员工员工 员工)
{
Console.WriteLine( 员工ID: + employee.EmployeeID + ' \t' + < span class =code-string>员工姓名: + employee.EmployeeName);
Console.ReadLine();
}
}
hi,
I entered employee details in console while compiling, now i need to display those details in alist.
What I have tried:
namespace Program1 { class Employee { public string Empname = ""; public int EMPID = 0; //public void print() //{ // Console.WriteLine("\n Employee Id is " + EMPID); // Console.WriteLine("\n Employee Name is " + Empname); //} //Declare Employee ID public int EmployeeID { get; set; } //Declare Employee Name public string EmployeeName { get; set; } //public override string ToString() //{ // for (int i = 0; i < 24; i++) //just user the static array length // { // EMPID[i].Number = Console.ReadLine(); // Empname[i].Name = Console.ReadLine(); // } // return "EmployeeID =" + EMPID + " , EmployeeName = " + Empname; //} // foreach (var item in newlist.OrderBy(Employee => Employee.empid)) Console.WriteLine(Employee); } class Program { public static void Main() { // Employee e = new Employee();// create object with employee // //e.EmployeeID = 501; // //e.EmployeeName = "Anusha"; // //Console.WriteLine(" Employee Data:{0}", e.EmployeeID); // //Console.WriteLine(" Employee Data:{0}", e.EmployeeName); // //Console.ReadLine(); //for (int i = 0; i<2; i++) // { // Console.WriteLine("Enter Employee ID", e.EmployeeID); // Console.ReadLine(); // Console.WriteLine("Enter Employee name", e.EmployeeName); // Console.ReadLine(); // string line = Console.ReadLine(); // if (line == "exit") // { // break; // } //} //Console.WriteLine("Employee ID :" + e.EMPID + '\t' + "Employee Name :" + e.Empname); //Console.ReadLine(); Console.WriteLine(" Enter Employee ID"); var EmpID = int.Parse(Console.ReadLine()); var employee1 = new Employee { EmployeeID= EmpID; } } } //public class Book //{ // public string BookName { get; set; } = string.Empty; // public string Author { get; set; } = string.Empty; // public string ISBN { get; set; } = string.Empty; //} }
Your problem is here:
public static void Main() { Employee e = new Employee();// create object with employee for (int i = 0; i<2; i++) { Console.WriteLine("Enter Employee ID", e.EmployeeID); Console.ReadLine(); Console.WriteLine("Enter Employee name", e.EmployeeName); Console.ReadLine(); string line = Console.ReadLine(); if (line == "exit") { break; } } Console.WriteLine("Employee ID :" + e.EMPID + '\t' + "Employee Name :" + e.Empname); Console.ReadLine(); }You only ever create one instance of an Employee - so inside your loop all you could do (if your code did anything, and it won't even compile at the moment, much less do anything useful) - is overwrite the content you already have.
It won't compile because your EMployee class includes code that isn't inside a method:
class Employee { public string Empname = ""; public int EMPID = 0; public int EmployeeID { get; set; } public string EmployeeName { get; set; } Console.WriteLine(Employee); <<<---- This line isn't in a method }
But you really need to go back to basics and start again...
class Employee { public int EmployeeID { get; set; } public string EmployeeName { get; set; } }Will do for your Employee class.
Now, to use it:
public static void Main() { List<Employee> employees = new List<Employee>(); for (int i = 0; i<2; i++) { Employee employee = new Employee(); Console.WriteLine("Enter Employee name", e.EmployeeName); string line = Console.ReadLine(); if (line == "exit") { break; } ... Fill in Employee details here employees.Add(employee); } foreach (Employee employee in employees) { ... print Employee details here } Console.ReadLine(); }
You need to create a list, and add the created Employee objects to that list during each loop. Then at the end you can work with the list you created.
public static void Main() { List<Employee> employees = new List<Employee>(); for (int i = 0; i<2; i++) { Employee e = new Employee();// create object with employee Console.WriteLine("Enter Employee ID:"); e.EmployeeID = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Employee name:"); e.EmployeeName = Console.ReadLine(); employees.Add(e); string line = Console.ReadLine(); if (line == "exit") { break; } } foreach (Employee employee in employees) { Console.WriteLine("Employee ID :" + employee.EmployeeID + '\t' + "Employee Name :" + employee.EmployeeName); Console.ReadLine(); } }
这篇关于我们如何在列表中的控制台中显示输入的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!