我正在尝试用户输入和显示

时间:2014-11-28 17:44:48

标签: c#

这是我的代码。我试图获取用户输入并显示FoodItem,但我这样做的方式只允许输入一个输入而不在while循环中执行大量的“或”语句。还可以根据输入打印配置文件。有没有办法打印多次?

namespace excercise_4_week_6
{
    class Program
    {
        double totalCost;
        string inputString = "";

        void run()
        {
            Console.WriteLine("==++     Food Menu     ++==");
            Console.WriteLine();

            Console.WriteLine("Please enter the item you would like to order: ");

            Console.WriteLine();

            FoodItem Pizza = new FoodItem("Pizza", "Margherita Pizza", 4, 9.99);
            FoodItem Burger = new FoodItem("Burger", "Burger with Cheese and chips", 1, 8.99);

            while (inputString == "" || inputString == Pizza.Name)
                {
                    inputString = Console.ReadLine();
                    Console.WriteLine();

                    if (inputString == Pizza.Name)
                    {
                        Pizza.PrintFoodProfile();
                        totalCost = totalCost + Pizza.Cost;
                        Console.WriteLine("The total cost is: £" + totalCost);
                        Console.WriteLine();
                    }
                }
        }

        static void Main(string[] args)
        {
          Program excercise_4_week_6 = new Program();
          excercise_4_week_6.run();
       }
    }

    public class FoodItem
    {
        string name;
        string description;
        int numberServed;
        double cost;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public int NumberServed
        {
            get { return numberServed; }
            set { numberServed = value; }
        }

        public double Cost
        {
            get { return cost; }
            set { cost = value; }
        }

        public FoodItem(string name, string description, int numberServed, double cost)
        {
            this.name = name;
            this.description = description;
            this.numberServed = numberServed;
            this.cost = cost;
        }

        public void PrintFoodProfile()
        {
            Console.WriteLine("Name: " + name);
            Console.WriteLine("Description: " + description);
            Console.WriteLine("The number of people this serves: " + numberServed);
            Console.WriteLine("Cost: £" + cost);
            Console.WriteLine("---------------------------------------");
            Console.WriteLine();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

将食物放入字典:

var foodItems =
    new Dictionary<string, FoodItem>(StringComparer.CurrentCultureIgnoreCase);

foodItem.Add("Pizza", new FoodItem("Pizza", "Margherita Pizza", 4, 9.99));
foodItem.Add("Burger", new FoodItem("Burger", "Burger with Cheese & chips", 1, 8.99));

现在您可以按名称查找食品:

FoodItem foodItem;
if (foodItems.TryGetValue(userInput, out foodItem)) {
    Console.WriteLine(foodItem.Name);
} else {
    Console.WriteLine("not found!");
}

您可以循环浏览所有食物:

double totalCost = 0;
foreach (FoodItem foodItem in foodItems.Values) {
    totalCost += foodItem.Cost;
    Console.WriteLine("{0} = {1:0.00}", foodItem.Name, foodItem.Cost);
}
Console.WriteLine("Total cost = {1:0.00}", foodItem.Cost);

如果您只需要遍历食品,但不必按名称查找单个商品,那么将食品添加到List<FoodItem>就足够了。

答案 1 :(得分:0)

你想要删除吗?

而不是

 while (inputString == "" || inputString == Pizza.Name)
  {
      inputString = Console.ReadLine();
      Console.WriteLine();

      if (inputString == Pizza.Name)
      {
          Pizza.PrintFoodProfile();
          totalCost = totalCost + Pizza.Cost;
          Console.WriteLine("The total cost is: £" + totalCost);
          Console.WriteLine();
      }
  }

使用此

    if (inputString == Pizza.Name)
   {
       Pizza.PrintFoodProfile();
       //get NumberServed
       Pizza.NumberServed = Console.ReadLine();
       totalCost = Pizza.NumberServed * Pizza.Cost;
   }
   else if (inputString == Burger.Name)
   {
       Burger.PrintFoodProfile();
       //get NumberServed
       Burger.NumberServed = Console.ReadLine();
       totalCost = Burger.NumberServed * Burger.Cost;
    }
    Console.WriteLine("The total cost is: £" + totalCost);

用于删除