c# - C# 在通用列表中列出用户输入
问题描述
所以,我正在研究 C# 来提高自己。我正在研究通用列表,我只是试图询问用户两个选择。1 显示学生列表,2- 将学生添加到列表中(ID 是自动生成的,用户只需提供姓名)。当我运行代码时,列表总是空的。我在哪里做错了你们能帮忙吗?
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; }
}
}
解决方案
你的尝试看起来有点像意大利面条代码,所以我采取了自由并重写了一点。还有几个问题。我添加了一些评论以进行进一步解释。
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;
}
}
推荐阅读
- encryption - 如何使蓝牙连接安全?
- javascript - 请 PHP 不从 json 发布数据
- sql-server - 尝试通过 SQL 代理作业执行 powershell 代码时出错
- authentication - Hide newly released application temporarly for users in PROD
- angular-material - 角材料步进器删除图标
- css - 隐藏元素在移动设备上触发 onClick
- node.js - SyntaxError: 非法的 continue 语句:没有环绕的迭代语句
- postgresql - 当我启动 nominatim 时如何查看地图?
- python-3.x - 结合pandas“data frame.style”对象并输出到html
- java - 测试文件夹在 Spring Boot 项目中的作用是什么?