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



我想制作一种菜单,您可以选择菜,输入菜的数量,然后将菜肴添加到您的列表中。

这就像它的简单版本,但是

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>

制作一个字典以保留您的菜单:

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类的字典用作值的类型。这使您不仅可以将菜的名字关联(我认为您不榨wan来输出该名称,但请选择进一步的操作:

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用作取回它们的钥匙。另外,如果要将菜肴存储在数据库中,则可以轻松地将此类转换为实体框架实体。

编辑

循环中的菜肴选择 - 这允许选择多种菜肴。

正如帕特里克·阿图纳(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#小提琴

最新更新