我正在創建一個控制臺應用程式,它使用用戶輸入來確定兩個串列中的資料。每個串列具有相同的資料物件。它們是名稱、rollNo、課程和流。
當用戶輸入了所有資料后,系統會詢問他們希望查看哪些流資訊。然后我希望顯示與流輸入相關的資料。例如,如果他們想查看流 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 陳述句,但我不知道這是否是最好的方法。
uj5u.com熱心網友回復:
讓我們分解解決方案,讓我們從讀取整數開始:
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.");
}
}
然后我們可以實作ReadStudentand ReadTrainer:
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}");
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/481955.html
上一篇:使用高階函式時無法傳遞引數
