首页 > 解决方案 > 列表在循环和 switch 语句中被覆盖

问题描述

我需要帮助。我不知道如何解释我的问题。我之前尝试过发布一次,但它被关闭了,因为我无法真正解释我的问题。我正在尝试创建一个应用程序,用户可以在其中输入汽车值,例如汽车 ID、汽车容量(以千克为单位)和汽车登记号。我声明一个这样的类:

public class Car
{
    public string ID { get; set; }
    public string capacityKg { get; set; }
    public string reachKm { get; set; }
    public string registrationNumber { get; set; }
}

然后我在我的代码中的循环外声明我的列表和类,如下所示:

List<Car> allCars = new List<Car>();
Car vehicle = new Car();

然后我在这样的 switch 语句中使用 do while 循环来每次从用户那里获取值:

do
{
    vehicle = new Car();

    Write("ID: ");
    vehicle.ID = ReadLine();

    Write("Capacity (kg): ");
    vehicle.capacityKg = ReadLine();

    Write("Reach (km): ");
    vehicle.reachKm = ReadLine();

    Write("Registration number: ");
    vehicle.registrationNumber = ReadLine();

    Clear();

    WriteLine($ "ID: {vehicle.ID}");
    WriteLine($ "Capacity (kg): {vehicle.capacityKg}");
    WriteLine($ "Reach (km): {vehicle.reachKm}");
    WriteLine($ "Registration number: {vehicle.registrationNumber}");

    WriteLine(" ");
    WriteLine("Is this correct? (Y)es (N)o");

    ConsoleKeyInfo yesNo = ReadKey(true);

    if (yesNo.Key == ConsoleKey.Y)
    {
        allCars.Add(vehicle);

        Clear();

        WriteLine("Delivery unit registered");

        Thread.Sleep(2000);

        Clear();

        break;
    }
    else if (yesNo.Key == ConsoleKey.N)
    {
        Clear();
    }
    else
    {
        Clear();

        WriteLine("Invalid key pressed.");

        Thread.Sleep(2000);

        Clear();
    }
} while (invalidCarCredentials);

但是当我遍历列表时,我希望输出是这样的:

但是,当用户输入一个新值时,它会得到如下输出:

这意味着以前的值被新值覆盖。我不想要那个

这是我遍历列表以在控制台上打印出值的部分:

Clear();
Write("Search for Car by ID: ");
string searchForCar = ReadLine();

if (vehicle.ID == searchForCar)
{
    foreach (var veh in allCars)
    {
        WriteLine("Value found.");
        // write here all the information you want to display.
        WriteLine($"ID: {vehicle.ID}");
        WriteLine($"Capacity (kg): {vehicle.capacityKg}");
        WriteLine($"Reach (km): {vehicle.reachKm}");
        WriteLine($"Registration number: {vehicle.registrationNumber}");
    }
}
else
{
    WriteLine("Unit not found");
}

ReadKey(true);
Clear();
break;

标签: c#listloopsclassswitch-statement

解决方案


我已经测试了你的代码,它工作正常

static void Main(string[] args)
{
    List<Car> AllCars = new List<Car>();
    Car vehicle = new Car();

    do
    {
        vehicle = new Car();
        Console.WriteLine("ID: ");
        vehicle.ID = Console.ReadLine();
        Console.WriteLine("KG: ");
        vehicle.capacityKg = Console.ReadLine();
        Console.WriteLine("km: ");
        vehicle.reachKm = Console.ReadLine();
        Console.WriteLine("Number: ");
        vehicle.registrationNumber = Console.ReadLine();
        Console.WriteLine("Correct?");
        ConsoleKeyInfo yesNo = Console.ReadKey(true);

        if (yesNo.Key == ConsoleKey.Y)
        {
            AllCars.Add(vehicle);
        }
        else if (yesNo.Key == ConsoleKey.N)
        {
            gotCars = false;
        }

    } while (gotCars);

    foreach (Car item in AllCars)
    {
        Console.Write(item.registrationNumber + "  " + item.ID);
    }

    Console.Read();
}

我的列表输出是Car123-Car456-Car789(如果输入正确)


推荐阅读