在if和foreach语句中使用列表

  • 本文关键字:列表 语句 if foreach c# oop
  • 更新时间 :
  • 英文 :


我正在创建一个控制台应用程序,它使用用户输入来确定两个列表中的数据。每个列表都有相同的数据对象。它们是name, rollNo, course和stream。

当用户输入了所有的数据后,他们会被询问他们想要看到哪个流信息。然后,我希望显示与流输入相关的数据。例如,如果他们想要查看流1,那么该流上的培训师和学生的信息应该出现在控制台中。

下面是我的代码

Student studentOne = new Student();
Console.WriteLine("Enter the Name of Student:");
studentOne.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of Student:");
studentOne.rollNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of Student:");
studentOne.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of Student:");
studentOne.stream = int.Parse(Console.ReadLine());
Student studentTwo = new Student();
Console.WriteLine("Enter the Name of Student:");
studentTwo.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of Student:");
studentTwo.rollNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of Student:");
studentTwo.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of Student:");
studentTwo.stream = int.Parse(Console.ReadLine());
Student studentThree = new Student();
Console.WriteLine("Enter the Name of Student:");
studentThree.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of Student:");
studentThree.rollNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of Student:");
studentThree.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of Student:");
studentThree.stream = int.Parse(Console.ReadLine());
var studentData = new List<Student>();
studentData.Add(studentOne);
studentData.Add(studentTwo);
studentData.Add(studentThree);
Trainer trainerOne = new Trainer();
Console.WriteLine("Enter the Name of the trainer:");
trainerOne.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of the trainer:");
trainerOne.trainNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of the trainer:");
trainerOne.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of the trainer:");
trainerOne.stream = int.Parse(Console.ReadLine());
Trainer trainerTwo = new Trainer();
Console.WriteLine("Enter the Name of the trainer:");
trainerTwo.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of the trainer:");
trainerTwo.trainNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of the trainer:");
trainerTwo.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of the trainer:");
trainerTwo.stream = int.Parse(Console.ReadLine());
Trainer trainerThree = new Trainer();
Console.WriteLine("Enter the Name of the trainer:");
trainerThree.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of the trainer:");
trainerThree.trainNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of the trainer:");
trainerThree.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of the trainer:");
trainerThree.stream = int.Parse(Console.ReadLine());
List<Trainer> trainerData = new List<Trainer>();
trainerData.Add(trainerOne);
trainerData.Add(trainerTwo);
trainerData.Add(trainerThree);
Console.WriteLine("Which stream details do you want to view?");
var streamInfo = int.Parse(Console.ReadLine());
if (streamInfo == 1)
{
foreach (var stream in trainerData) 
{
}
}
class Student
{
public string name { get; set; }
public int rollNo { get; set; }
public string course { get; set; }
public int stream { get; set; }

}
class Trainer
{
public string name { get; set; }
public int trainNo { get; set; }
public string course { get; set; }
public int stream { get; set; }
}

任何帮助都是感激的。

编辑:我没有说清楚我要求的是什么。我不确定如何根据流输入显示正确的数据。我试过使用if语句和foreach语句,但我不知道这是否是最好的方法。

让我们分解解,让我们从读取整数开始:

private static int ReadInteger(string title, Func<int, bool> isValid = null) {
if (isValid is null)
isValid = x => true;  
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
if (int.TryParse(Console.ReadLine(), out int result) && isValid(result))
return result;
Console.WriteLine("Invalid value, please, try again.");
}
}
private static string ReadString(string title, Func<string, bool> isValid = null) {
if (isValid is null)
isValid = x => true;  
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
string result = Console.ReadLine(); 
if (isValid(result))
return result;
Console.WriteLine("Invalid value, please, try again.");
}
}

然后我们可以实现ReadStudentReadTrainer:

public static Student ReadStudent() {
return new Student() {
name   = ReadString("Enter the Name of Student:", x => !string.IsNullOrWhiteSpace(x)),
rollNo = ReadInteger("Enter the roll number of Student:", x => x >= 0),
course = ReadString("Enter the course of Student:", x => !string.IsNullOrWhiteSpace(x)),
stream = ReadInteger("Enter the Stream of Student:", x => x >= 0),
};
}
public static Trainer ReadTrainer() {
return new Trainer() {
name   = ReadString("Enter the Name of trainer:", x => !string.IsNullOrWhiteSpace(x)),
rollNo = ReadInteger("Enter the roll number of trainer:", x => x >= 0),
course = ReadString("Enter the course of trainer:", x => !string.IsNullOrWhiteSpace(x)),
stream = ReadInteger("Enter the Stream of trainer:", x => x >= 0),
};
}

最后,我们可以创建尽可能多的学生和培训师(注意for循环-如果我们想进入100学生怎么办?):

int students = 3;
int trainers = 3;
var studentData = new List<Student>(students);
for (int i = 0; i < students; ++i)
studentData.Add(ReadStudent());
var trainerData = new List<Trainer>(trainers);
for (int i = 0; i < trainers; ++i)
trainerData.Add(ReadTrainer());

最后,要按流查询列表,您可以使用Linq或好的旧循环或Linq:

int stream = ReadInteger("Which stream details do you want to view?");
foreach (Student s in studentData)
if (s.stream == stream)
Console.WriteLine($"Student {s.name} Roll No {s.rollNo}");
foreach (Trainer t in trainerData)
if (t.stream == stream)
Console.WriteLine($"Trainer {t.name} Roll No {t.rollNo}");