C#在通用列表中列出用户输入
因此,我正在学习C#来提高自己。我正在研究通用列表,我只是试着问用户两个选择。1显示学生列表,2-向列表中添加学生(Id自动生成,用户只需给出姓名)。当我运行代码时,列表总是空的。我哪里搞错了你们能帮忙吗C#在通用列表中列出用户输入,c#,visual-studio,C#,Visual Studio,因此,我正在学习C#来提高自己。我正在研究通用列表,我只是试着问用户两个选择。1显示学生列表,2-向列表中添加学生(Id自动生成,用户只需给出姓名)。当我运行代码时,列表总是空的。我哪里搞错了你们能帮忙吗 class Program { static void Main(string[] args) { AddStudent addStudent = new AddStudent(); PrintStudentsList prin
class Program
{
static void Main(string[] args)
{
AddStudent addStudent = new AddStudent();
PrintStudentsList printStudentsList = new PrintStudentsList();
int choice;
do
{
Console.WriteLine("1-See the student list\n2-Add a student to the list\n3-Exit");
choice = Int32.Parse(Console.ReadLine());
if (choice == 1)
{
printStudentsList.PrintStudents();
}
else if (choice == 2)
{
Console.Write("Enter the Name of the Student please: ");
string studentName = Console.ReadLine();
addStudent.Add(studentName);
}
else if (choice==3)
{
Console.WriteLine("Exiting from the program");
}
else
{
Console.WriteLine("You entered a wrong choice, please try again...");
}
}
while (choice!=3);
Console.ReadLine();
}
}
class AddStudent
{
private List<Students> students = new List<Students>();
public List<Students>GetList()
{
return students;
}
private string _studentName;
//public AddStudent(String studentName)
//{
// _studentName = studentName;
//}
public void Add(string studentName)
{
Random rnd = new Random();
int idNum = rnd.Next(1, 100);
students.Add(
new Students {Id =idNum, Name = studentName});
}
}
class PrintStudentsList
{
private AddStudent addStudent = new AddStudent();
public void PrintStudents()
{
addStudent.GetList();
if (addStudent.GetList().Count==0)
{
Console.WriteLine( new EmptyListException("Student List is Empty!!!"));
}
else
{
foreach (var student in addStudent.GetList())
{
Console.Write(student);
}
}
}
}
class EmptyListException:Exception
{
public EmptyListException(string Message):base(Message)
{
}
}
class Students
{
public int Id { get; set; }
public string Name { get; set; }
}
}
类程序
{
静态void Main(字符串[]参数)
{
AddStudent AddStudent=新的AddStudent();
PrintStudentsList PrintStudentsList=新的PrintStudentsList();
智力选择;
做
{
Console.WriteLine(“1-查看学生列表\n2将学生添加到列表\n3退出”);
choice=Int32.Parse(Console.ReadLine());
如果(选项==1)
{
printStudentsList.PrintStudents();
}
else if(选项==2)
{
控制台。写(“请输入学生姓名:”);
string studentName=Console.ReadLine();
addStudent.Add(studentName);
}
else if(选项==3)
{
Console.WriteLine(“退出程序”);
}
其他的
{
Console.WriteLine(“您输入了错误的选项,请重试…”);
}
}
while(选项!=3);
Console.ReadLine();
}
}
班级学生
{
私人名单学生=新名单();
公共ListGetList()
{
留学生;
}
私有字符串\u studentName;
//public AddStudent(字符串studentName)
//{
//_studentName=studentName;
//}
public void Add(字符串studentName)
{
随机rnd=新随机();
intidnum=rnd.Next(1100);
学生们,加上(
新生{Id=idNum,Name=studentName});
}
}
班级学生名单
{
private AddStudent AddStudent=new AddStudent();
公立学校学生()
{
addStudent.GetList();
如果(addStudent.GetList().Count==0)
{
Console.WriteLine(新的EmptyListException(“学生列表为空!!!”);
}
其他的
{
foreach(addStudent.GetList()中的var student)
{
控制台。写(学生);
}
}
}
}
类EmptyListException:异常
{
公共EmptyListException(字符串消息):基本(消息)
{
}
}
班级学生
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
}
您的尝试看起来有点像意大利面代码,所以我获得了自由,重新编写了一点。还有几个问题。为了进一步解释,我添加了一些注释
using System;
using System.Collections.Generic;
public class Student
{
public string Name { get; set; }
public int Id { get; set; }
// Constructors are better for the programmer, because he knows what variables are required
// And with this one, a stundent will always have a name and an id
public Student(string name)
{
Name = name;
Id = Program.rnd.Next(1, 100);
}
// Important, otherwise Console.WriteLine(student) only outputs "object"
public override string ToString()
{
string result = $"Id: {Id} Name: {Name}";
return result;
}
}
public class StudentList
{
List<Student> students;
public StudentList()
{
students = new List<Student>();
}
using System;
using System.Collections.Generic;
public class Student
{
public string Name { get; set; }
public int Id { get; set; }
// Hide the default constructor, because otherwise one might forget setting the name or the id
private Student()
{
}
// Constructors are better for the programmer, because he knows what variables are required
// And with this one, a stundent will always have a name and an id
public Student(string name)
{
Name = name;
Id = Program.rnd.Next(1, 100);
}
// Important, otherwise Console.WriteLine(student) only outputs "object"
public override string ToString()
{
string result = $"Id: {Id} Name: {Name}";
return result;
}
}
public class StudentList
{
List<Student> list;
public StudentList()
{
list = new List<Student>();
}
public List<Student> List
{
get
{
return list;
}
// Dont allow setting the list from outside, so no set here
//set
//{
// students = value;
//}
}
public void PrintList()
{
Console.WriteLine("List of students: ");
foreach (Student student in list)
{
Console.WriteLine(student);
}
Console.WriteLine();
}
}
class Program
{
// Make sure you only create one instance of Random, don't instantiate it whenever you need a new value. Otherwise you risk getting the same results
public static Random rnd = new Random();
static void Main(string[] args)
{
StudentList students = new StudentList();
int choice;
do
{
Console.WriteLine("1-See the student list\n2-Add a student to the list\n3-Exit");
// Never use .Parse, because it throws exceptions on invalid input
// choice = Int32.Parse(Console.ReadLine());
string input = Console.ReadLine();
if (int.TryParse(input, out choice) == false)
{
// if parsing didn't work, set the choice to any value that is not one of the cases in belows switch statement
// it will then be handled by the default switch case
choice = -1345;
}
// Switches are easier to read then too many else ifs
switch (choice)
{
case 1:
// Don't throw Exceptions when you don't have to, you could do something like this instead
if (students.List.Count == 0)
{
Console.WriteLine("There are no students");
}
else
{
students.PrintList();
}
break;
case 2:
Student student = GetStudent();
students.List.Add(student);
break;
// Nothing to do here, it's just to prevent entering the default case
case 3:
break;
default:
Console.WriteLine("You entered a wrong choice, please try again...");
break;
}
Console.WriteLine();
} while (choice != 3);
Console.WriteLine("Exiting from the program");
Console.ReadLine();
}
private static Student GetStudent()
{
Console.Write("Enter the Name of the Student please: ");
string studentName = Console.ReadLine();
Student student = new Student(studentName);
return student;
}
}
使用系统;
使用System.Collections.Generic;
公立班学生
{
公共字符串名称{get;set;}
公共int Id{get;set;}
//构造函数对程序员来说更好,因为他知道需要哪些变量
//有了这个,特技登特将永远有一个名字和一个id
公立学生(字符串名称)
{
名称=名称;
Id=Program.rnd.Next(1100);
}
//重要信息,否则为控制台。WriteLine(学生)仅输出“对象”
公共重写字符串ToString()
{
字符串结果=$“Id:{Id}名称:{Name}”;
返回结果;
}
}
公共班级学生名单
{
列出学生名单;
公立学生名单()
{
学生=新列表();
}
使用制度;
使用System.Collections.Generic;
公立班学生
{
公共字符串名称{get;set;}
公共int Id{get;set;}
//隐藏默认构造函数,否则可能会忘记设置名称或id
私立学生()
{
}
//构造函数对程序员来说更好,因为他知道需要哪些变量
//有了这个,特技登特将永远有一个名字和一个id
公立学生(字符串名称)
{
名称=名称;
Id=Program.rnd.Next(1100);
}
//重要信息,否则为控制台。WriteLine(学生)仅输出“对象”
公共重写字符串ToString()
{
字符串结果=$“Id:{Id}名称:{Name}”;
返回结果;
}
}
公共班级学生名单
{
名单;
公立学生名单()
{
列表=新列表();
}
公开名单
{
得到
{
退货清单;
}
//不允许从外部设置列表,因此此处不设置
//设置
//{
//学生=价值观;
//}
}
公共作废打印列表()
{
Console.WriteLine(“学生名单:”);
foreach(列表中的学生)
{
控制台。WriteLine(学生);
}
Console.WriteLine();
}
}
班级计划
{
//确保只创建一个Random实例,不要在需要新值时实例化它。否则可能会得到相同的结果
公共静态随机rnd=新随机();
静态void Main(字符串[]参数)
{
StudentList students=new StudentList();
智力选择;
做
{
Console.WriteLine(“1-查看学生列表\n2将学生添加到列表\n3退出”);
//永远不要使用.Parse,因为它会对无效输入抛出异常
//choice=Int32.Parse(Console.ReadLine());
字符串输入=Console.ReadLine();
if(int.TryParse(输入,输出选项)=false)
{
//如果解析不起作用,请将该选项设置为belows switch语句中不包含的任何值
//然后,它将由默认的开关情况处理
选择=-1345;
}
//开关更容易打开
using System;
using System.Collections.Generic;
public class Program
{
// Make sure you only create one instance of Random, don't instantiate it whenever you need a new value. Otherwise you risk getting the same results
public static Random rnd = new Random();
public static void Main(string[] args)
{
var students = new List<Student>();
int choice;
do
{
Console.WriteLine("1-See the student list\n2-Add a student to the list\n3-Exit");
// Never use .Parse, because it throws exceptions on invalid input
// choice = Int32.Parse(Console.ReadLine());
string input = Console.ReadLine();
if (int.TryParse(input, out choice) == false)
{
// if parsing didn't work, set the choice to any value that is not one of the cases in belows switch statement
// it will then be handled by the default switch case
choice = -1345;
}
// Switches are easier to read then too many else ifs
switch (choice)
{
case 1:
// Don't throw Exceptions when you don't have to, you could do something like this instead
if (students.Count == 0)
{
Console.WriteLine("There are no students");
}
else
{
students.PrintList();
}
break;
case 2:
Student student = GetStudent();
students.Add(student);
break;
// Nothing to do here, it's just to prevent entering the default case
case 3:
break;
default:
Console.WriteLine("You entered a wrong choice, please try again...");
break;
}
Console.WriteLine();
} while (choice != 3);
Console.WriteLine("Exiting from the program");
}
private static Student GetStudent()
{
Console.Write("Enter the Name of the Student please: ");
string studentName = Console.ReadLine();
Student student = new Student(studentName);
return student;
}
}
public class Student
{
public string Name { get; set; }
public int Id { get; set; }
// Hide the default constructor, because otherwise one might forget setting the name or the id
private Student()
{
}
// Constructors are better for the programmer, because he knows what variables are required
// And with this one, a stundent will always have a name and an id
public Student(string name)
{
Name = name;
Id = Program.rnd.Next(1, 100);
}
// Important, otherwise Console.WriteLine(student) only outputs "object"
public override string ToString()
{
//string result = $"Id: {Id} Name: {Name}";
string result = String.Format("Id: {0} Name: {1}", Id, Name);
return result;
}
}
public static class StudentListExtensions
{
public static void PrintList(this List<Student> list)
{
Console.WriteLine("\nList of students: ");
foreach (Student student in list)
{
Console.WriteLine(student);
}
Console.WriteLine("\n");
}
}
using System;
using System.Collections.Generic;
namespace ConsoleApp2
{
class Program
{
private static List<Students> studentsList = new List<Students>();
static void Main(string[] args)
{
int choice;
do
{
Console.WriteLine("1-See the student list\n2-Add a student to the list\n3-Exit");
choice = Int32.Parse(Console.ReadLine());
if (choice == 1)
{
PrintStudents();
}
else if (choice == 2)
{
Console.Write("Enter the Name of the Student please: ");
string studentName = Console.ReadLine();
Add(studentName);
}
else if (choice == 3)
{
Console.WriteLine("Exiting from the program");
}
else
{
Console.WriteLine("You entered a wrong choice, please try again...");
}
} while (choice != 3);
Console.ReadLine();
}
static void Add(string studentName)
{
Random rnd = new Random();
int idNum = rnd.Next(1, 100);
studentsList.Add(
new Students { Id = idNum, Name = studentName });
}
static void PrintStudents()
{
if (studentsList.Count == 0)
{
Console.WriteLine(new EmptyListException("Student List is Empty!!!"));
}
else
{
foreach (var student in studentsList)
{
Console.WriteLine($"student Number:{student.Id} StudentName:{student.Name}");
}
}
}
}
class EmptyListException : Exception
{
public EmptyListException(string Message) : base(Message)
{
}
}
class Students
{
public int Id { get; set; }
public string Name { get; set; }
}
}