我的字典没有保存程序中的值

msh*_*msh 1 c# dictionary

我想创建一个程序,我可以根据汽车的型号、类型和车牌保存汽车。我尝试将所有这些信息保存在字典中,但是当我通过第二个选项(“2. 搜索车辆”)搜索它们时,它们没有被保存。这是我的代码:

我应该怎么做才能让我的代码正常工作?我希望能够通过他们的车牌(瑞典车牌,ABC123)搜索汽车,但我无法弄清楚为什么没有保存我通过 ReadLines() 输入的字典值

Dictionary<string, string> newVehicle = new Dictionary<string, string>();

bool sunfleetAdminTools = true;

Dictionary<string, string> newVehicle = new Dictionary<string, string>();

do
{
    WriteLine("1. Add Vehicle");

    WriteLine("2. Search Vehicle");

    WriteLine("3. Log Out");

    ConsoleKeyInfo keyInput = ReadKey(true);

    Clear();

    switch (keyInput.Key)
    {
        case ConsoleKey.D1:

            Clear();

            bool invalidCarCredentials = true;

            do
            {
                Write("Registration Number: ");
                string regNumber = ReadLine();
                newVehicle["Registration Number"] = regNumber;

                Write("Brand: ");
                string brand = ReadLine();
                newVehicle["Brand"] = brand;

                Write("Model: ");
                string vehicleModel = ReadLine();
                newVehicle["Model: "] = vehicleModel;

                Write("Type (Sedan, Compact, Subcompact): ");
                string vehicleType = ReadLine();
                newVehicle["Type: "] = vehicleType;

                Write("Autopilot (Yes, No): ");
                string autoPilot = ReadLine();
                newVehicle["Autopilot: "] = autoPilot;

                Clear();

                foreach (KeyValuePair<string, string> kvp in newVehicle)
                {
                    WriteLine("Car {0}: {1}", kvp.Key, kvp.Value);
                }

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

                ConsoleKeyInfo newInput = ReadKey(true);

                if (newInput.Key == ConsoleKey.Y)
                {
                    Clear();

                    WriteLine("Vehicle registered.");

                    Thread.Sleep(2000);

                    Clear();

                    break;
                }
                else if (newInput.Key == ConsoleKey.N)
                {
                    Clear();
                }
            } while (invalidCarCredentials);

            break;
        case ConsoleKey.D2:
            Write("Search for vehicle by license plate: ");
            string searchingForVehicle = ReadLine();

            if (newVehicle.ContainsValue(searchingForVehicle))
            {
                WriteLine("Value found.");
                foreach (KeyValuePair<string, string> kvp in newVehicle)
                {
                    WriteLine("Car {0}: {1}", kvp.Key, kvp.Value);
                }
            }
            else
            {
                WriteLine("Car not found.");
                Thread.Sleep(2000);
            }

            Clear();

            break;
        case ConsoleKey.D3:
            Clear();

            WriteLine("Logging out...");

            Thread.Sleep(1000);

            sunfleetAdminTools = false;

            Environment.Exit(0);

            break;
    }

} while (sunfleetAdminTools);
Run Code Online (Sandbox Code Playgroud)

Mon*_*Zhu 5

但他们没有得救

你进入了一个有趣的字典句法糖陷阱。它与您尝试向字典添加值的方式有关:

 newVehicle["Registration Number"] = regNumber;
Run Code Online (Sandbox Code Playgroud)

如果字典为空,则将"Registration Number"创建键并将其保存regNumber为键值对。另一方面,如果这个键已经存在,那么这个键上的值将被简单地覆盖!

这就是为什么您的问题出现并且您得到值未保存的印象的原因。您应该始终能够找到您输入的最后一个值。

如果您使用 oldscool 添加值的方法:

newVehicle.Add("Registration Number", regNumber);
Run Code Online (Sandbox Code Playgroud)

这将导致密钥已经存在的异常。你会立即意识到这是行不通的。

我应该怎么做才能让我的代码正常工作?

您应该添加一种面向对象的方法并将所有属于一个类的信息封装起来:

public class VehicleRepresentation
{
    public string RegistrationNumber { get; set; }
    public string Brand { get; set; }
    public string Model { get; set; }
    public string Type { get; set; }
    public bool AutoPilot { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以使用 aList<VehicleRepresentation> allVehicles来收集您的所有汽车。

VehicleRepresentation vehicle = new VehicleRepresentation();

Console.Write("Registration Number: ");
vehicle.RegistrationNumber = Console.ReadLine();                    

Console.Write("Brand: ");
vehicle.Brand = Console.ReadLine();

Console.Write("Model: ");
vehicle.Model = Console.ReadLine();

Console.Write("Type (Sedan, Compact, Subcompact): ");
vehicle.Type = Console.ReadLine();

Console.Write("Autopilot (Yes, No): ");
vehicle.AutoPilot = Console.ReadLine() == "Yes";

Console.Clear();

allVehicles.Add(vehicle);
Run Code Online (Sandbox Code Playgroud)

编辑:

WSC 的补充:

通常,您希望创建一个enumforvehicle.Type而不是用户输入的字符串列表。然而,在控制台环境中从用户输入中获取它可能有点麻烦。

搜索:

可以以不同的方式实现搜索。如果你想保持循环

foreach(var veh in allVehicles)
{
    if(veh.RegistrationNumber == searchingForVehicle)
    {
        WriteLine("Value found.");
        // write here all the information you want to display.
    }
}
Run Code Online (Sandbox Code Playgroud)

您当然也可以使用 LINQ 进行过滤。但我想你可以自己弄清楚。