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

时间:2017-10-05 12:50:37

标签: 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();

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

谢谢!

3 个答案:

答案 0 :(得分:0)

如果每个项目只出现一次,您可以使用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();

答案 1 :(得分:0)

您可以使用带有NoSuchElementException类的字典作为值的类型。这可以让你不仅仅关联这道菜的名字(我假设你不是要输出名字,而是根据你的选择做一些进一步的行动:

Dish

答案 2 :(得分:0)

我会介绍一个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# Fiddle