繁体   English   中英

按索引检索列表项后更新详细信息

[英]Update details after retrieving the list item by index

通过索引检索列表项后我无法更新详细信息(检索列表项后我应该如何更新详细信息?)

try
{
    Console.WriteLine("Updating the student details");
    Console.WriteLine("Enter the index at which to update a student details");
    
    int index = Convert.ToInt32(Console.ReadLine());
    for (int i = 0; i < Student.Count; i++)
    {
        if(i==index)
        {
            Console.WriteLine("Id:{0} \nName:{1} \nMarks:{2}", Student[i].Name, Student[i].ID, Student[i].Marks);
        }
        Console.WriteLine("What details you want to update?");
    }
}                   
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
break;

预期输出-->代码应该除了用户以 Int 形式输入并获取详细信息(通过使用索引)并将获取的详细信息返回给用户,然后用户应该能够修改详细信息并且修改后的详细信息应更新为学生详细信息列表。学生列表包含(字符串名称、整数 ID、整数标记)。

您可以使用带有键的菜单来更新学生的个人属性。

这是学生 class:

 public class Student { public Student(int id, string name, int marks) { ID = id; Name = name; Marks = marks; } public int ID { get; set; } public string? Name { get; set; } public int Marks { get; set; } public override string ToString() { return $"Id: {ID} \nName:{Name} \nMarks:{Marks}"; } }

我已覆盖 ToString() 以格式化打印学生详细信息。

将虚拟数据插入到 studentList 中,

 Random generator = new Random(); List<Student> studentList = new List<Student>(); for (int i = 1; i <= 5; i++) { studentList.Add( new Student(i, $"Student {i}", generator.Next(60, 101)) ); }

使用索引越界检查更新部分的实现,

 try { Console.WriteLine("Enter the index at which to update a student details"); int index = Convert.ToInt32(Console.ReadLine()); if (index >= studentList.Count) throw new Exception("Out of bound student index"); Console.WriteLine(studentList[index]); Console.WriteLine("What details you want to update?"); Console.WriteLine("1. ID\n2. Name\n3.Marks"); int choice = Convert.ToInt32(Console.ReadLine()); switch(choice) { case 1: Console.WriteLine("Enter new ID: "); int newId = Convert.ToInt32(Console.ReadLine()); studentList[index].ID = newId; break; case 2: Console.WriteLine("Enter new Name: "); string newName = Console.ReadLine()?? ""; studentList[index].Name = newName; break; case 3: Console.WriteLine("Enter new Marks: "); int newMarks = Convert.ToInt32(Console.ReadLine()); studentList[index].Marks = newMarks; break; default: Console.WriteLine("Invalid choice"); break; } Console.WriteLine(studentList[index]); } catch(Exception e) { Console.WriteLine(e.Message); }

抱歉,由于我是 StackOverflow 的新手,所以我找不到 pastebin,下面是我的代码,switch 语句 4 是我包含 SUB SWITCH 的地方,如果我错了,请指导我。

 using System; using System.Collections.Generic; namespace StudentList { class Student { public string Name { get; set; } public int ID { get; set; } public int Marks { get; set; } public Student(string Name, int ID, int Marks) { this.Name = Name; this.ID = ID; this.Marks = Marks; } } class Program { public static void Main() { List<Student> Student = new List<Student>(); Console.WriteLine("----Welcome to Student Management System----"); while (true) { Console.WriteLine("----Choose a Operation to perform----"); Console.WriteLine("1-->Create"); Console.WriteLine("2-->Read"); Console.WriteLine("3-->Delete"); Console.WriteLine("4-->Update\n"); int num = Convert.ToInt32(Console.ReadLine()); switch (num) { case 1: try { Console.WriteLine("Enter the Name:"); string Name = Convert.ToString(Console.ReadLine()); Console.WriteLine("Enter the ID:"); int ID = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the total marks:"); int Marks = Convert.ToInt32(Console.ReadLine()); Student.Add(new Student(Name, ID, Marks)); Console.WriteLine(); Console.WriteLine("\nAfter adding the Student,the list now contains:"); foreach (var std in Student) { Console.WriteLine("Name:" + std.Name); Console.WriteLine("ID:" + std.ID); Console.WriteLine("Marks:" + std.Marks); Console.WriteLine(" "); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case 2: if (Student.Count == 0) { Console.WriteLine( "There are no students in the list,Create Student details using Create option\n" ); } else { Console.WriteLine("Displaying the Student Details:"); foreach (var std in Student) { Console.WriteLine("Name:" + std.Name); Console.WriteLine("ID:" + std.ID); Console.WriteLine("Marks:" + std.Marks); } } break; case 3: try { if (Student.Count == 0) { Console.WriteLine( "There are no students in the list,Create Student details using Create option\n" ); } else { Console.WriteLine("Removing the student details"); Console.WriteLine("Enter the Index to remove a student details"); int remove = Convert.ToInt32(Console.ReadLine()); Student.RemoveAt(remove); Console.WriteLine( "After Removing a Student,the list now contains,\n" ); if (Student.Count == 0) { Console.WriteLine( "There are no students in the list,Create Student details using Create option\n" ); } foreach (var std in Student) { Console.WriteLine("Name:" + std.Name); Console.WriteLine("ID:" + std.ID); Console.WriteLine("Marks:" + std.Marks); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case 4: try { if (Student.Count == 0) { Console.WriteLine( "There are no students in the list,Create Student details using Create option\n" ); } else { Console.WriteLine("Updating the student details"); Console.WriteLine( "Enter the index at which to update a student details" ); if (Student.Count == 0) { Console.WriteLine( "There are no students in the list,Create Student details using Create option\n" ); } int index = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < Student.Count; i++) { if (i == index) { Console.WriteLine( "Id:{0} \nName:{1} \nMarks:{2}", Student[i].Name, Student[i].ID, Student[i].Marks ); } Console.WriteLine("Enter 1 to update"); Console.WriteLine( "Enter any number other than 1 if there is nothing to update" ); int n = Convert.ToInt32(Console.ReadLine()); Student.RemoveAt(n); switch (n) { case 1: Console.WriteLine("Enter the New Name:"); string Name = Convert.ToString(Console.ReadLine()); Console.WriteLine("Enter the New ID:"); int ID = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the New Total Marks:"); int Marks = Convert.ToInt32(Console.ReadLine()); Student.Add(new Student(Name, ID, Marks)); Console.WriteLine(); Console.WriteLine("\nUpdated"); Console.WriteLine(" "); break; default: Console.WriteLine("No Updation"); break; } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; default: Console.WriteLine("Invalid Choice,Please Choose a Number From 1 to 4"); break; } } } } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM