繁体   English   中英

C#控制台:读取数字输入并将其与字符串关联

[英]C# Console: Read number input and associate it with string

我想做一个菜单,您可以在其中选择一个菜式,输入菜式号,它将把菜式添加到您的列表中。

这就像一个简单的版本,但是

Console.WriteLine("CHINESE");
Console.Write("Type in the number of the dish you want: ");
int id = Convert.ToInt32(Console.ReadLine());

if (id == 35)
{
    Console.WriteLine("Pizza Funghi is added to your list.");
}
else
{
    Console.WriteLine($"{id} is not available.");
}

// I would like to use something like this instead of multiple if's
//35 = pizza funghi
//01 = pasta bolognese
//02 = pasta napolitana
//36 = pizza carbonara

Console.ReadKey();

你们能给我一些提示吗,我应该使用列表,字典还是数组? 还是应该使用课程?

谢谢!

如果每个项目仅出现一次Dictionary<int, string>可以使用Dictionary<int, string>

制作字典以保存菜单:

var menu = new Dictionary<int, string>
{
    { 35, "Pizza Funghi" },
    { 1, "Pasta Bolognese" },
    { 2, "Pizza Napolitana" },
    { 36, "Pizza Carbonara" }
};

然后您的代码如下所示:

Console.WriteLine("CHINESE");
Console.Write("Type in the number of the dish you want: ");
int id = Convert.ToInt32(Console.ReadLine());

if (menu.ContainsKey(id))
{
    Console.WriteLine($"{menu[id]} is added to your list.");
}
else
{
    Console.WriteLine($"{id} is not available.");
}

Console.ReadKey();

您可以使用带有Dish类的字典作为值的类型。 这样一来,您不仅可以为菜式关联一个名称(我假设您不需要输出该名称,但是可以对选择的内容做一些进一步的操作:

class Dish {
    string Name { get; set; }
    //Additional properties and/or methods would go here
}

//...

Dictionary<int, Dish> dishes = new Dictionary<int, Dish> {
    { 1, new Dish { Name = "Pasta Bolognese" } },
    { 2, new Dish { Name = "Pizza Napolitana" } },
    { 35, new Dish { Name = "Pizza Funghi" } },
    { 36, new Dish { Name = "Pizza Carbonara" } }
}

int id = Convert.ToInt32(Console.ReadLine());

//...

bool hasDish = dishes.TryGet( id, out Dish selectedDish);

if (hasDish)
{
    Console.WriteLine($"{selectedDish.Name} is added to your list.");
    //You can extend your Dish class with further properties and methods and use them here
}
else
{
    Console.WriteLine($"{id} is not available.");
}

我将介绍一个Dish类,它可以容纳价格等其他属性。 然后,使用以Dish ID为密钥的字典来检索它们。 同样,如果您想将餐具存储在数据库中,则可以轻松地将该类转换为Entity Framework实体。

编辑

循环运行菜肴选择-这允许选择多个菜肴。

如Patrick Artner所建议的,如果用户未输入数字,则失败。

using System;
using System.Collections.Generic;
using System.Linq;

public class Program {
    public static void Main() {

        var dishes = new List<Dish> {
            new Dish{ Id = 35, Name = "Pizza Funghi", Price = 11 },
            new Dish{ Id = 01, Name = "Pasta Bolognese", Price = 10 },
            new Dish{ Id = 02, Name = "Pasta Napolitana", Price = 9.5M },
            new Dish{ Id = 36, Name = "Pizza Carbonara", Price = 8 }
        };
        var dishesDict = dishes.ToDictionary(d => d.Id);

        var selectedDishes = new List<Dish>();

        Console.Write("Type in the number of the dish you want or x to stop: ");
        do {
            var input = Console.ReadLine();
            if (input.ToLowerInvariant().Trim() == "x") {
                break;
            }

            int id;
            if (!int.TryParse(input, out id)) {
                Console.WriteLine("Your input must be a number or 'x', please try again!");
                continue;
            }

            if (dishesDict.ContainsKey(id)) {
                var dish  = dishesDict[id];
                selectedDishes.Add(dish);
                Console.WriteLine(dish.Name + " is added to your list.");
            }
            else {  
                Console.WriteLine( id + " is not available.");
            }
        } while (true);

        // Example how to use additional property
        var totalPrice = selectedDishes.Sum(d => d.Price);
        Console.WriteLine("Total Price of selected dishes: " + totalPrice);
    }
}

public class Dish {

    public int Id { get; set; }
    public string Name { get; set; }
    // Example of additional property
    public decimal Price {get; set;}
}

C#小提琴

暂无
暂无

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

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