[英]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;}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.