C# 打印价格? 使用系统;使用System.Collections.Generic;使用System.Linq; 使用系统文本;使用System.Threading.Tasks; 名称空间分配12{ 班级计划 { 静态void Main(字符串[]参数) { 布尔发现; 串[]浇头={“奶酪”、“辣味香肠”、“青椒”、“洋葱”、“蘑菇”}; 双[]价格={1.30,1.90,1.80,1.70,1.60}; WriteLine(“选择一种配料:奶酪、意大利香肠、青椒、洋葱或蘑菇”); WriteLine(“请键入您想要的浇头-与上面显示的完全相同:”); 字符串顺序=Console.ReadLine(); Console.WriteLine(“\n您选择:“+订单”); 发现=错误; 对于(int i=0;i

C# 打印价格? 使用系统;使用System.Collections.Generic;使用System.Linq; 使用系统文本;使用System.Threading.Tasks; 名称空间分配12{ 班级计划 { 静态void Main(字符串[]参数) { 布尔发现; 串[]浇头={“奶酪”、“辣味香肠”、“青椒”、“洋葱”、“蘑菇”}; 双[]价格={1.30,1.90,1.80,1.70,1.60}; WriteLine(“选择一种配料:奶酪、意大利香肠、青椒、洋葱或蘑菇”); WriteLine(“请键入您想要的浇头-与上面显示的完全相同:”); 字符串顺序=Console.ReadLine(); Console.WriteLine(“\n您选择:“+订单”); 发现=错误; 对于(int i=0;i,c#,C#,当您搜索浇头时,您需要记住价格: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Assignment12 { class Program { static void Main(string[] args) { bool found

当您搜索浇头时,您需要记住价格:

using System; using System.Collections.Generic; using System.Linq;
using System.Text; using System.Threading.Tasks;

namespace Assignment12 {
   class Program
   {
       static void Main(string[] args)
       {
           bool found;
           string[] toppings = { "cheese", "pepperoni", "green pepper", "onion", "mushroom" };
           double[] price = { 1.30, 1.90, 1.80, 1.70, 1.60 };

           Console.WriteLine("Choose a topping: cheese, pepperoni, green pepper, onion or mushroom");
           Console.WriteLine("Please type the topping that you want - exactly as it appears above:");
           string order = Console.ReadLine();
           Console.WriteLine("\nYou chose: " + order);

           found = false;
           for (int i = 0; i < 5; i++)
           {
               if (order.Equals(toppings[i]))
               {
                   found = true;
               }
           }
           if (!found)
           {
               Console.WriteLine("The product " + order + " was not found, please try again!!!!!!\n");
               Console.WriteLine("Hit any key to close"); Console.ReadKey(true);
               return;
           }
// The problem is here with the local variable "price"
           Console.WriteLine("The price of " + order + " is $" + price);
// I also tried 1.30, 1.90, 1.80, 1.70, 1.60 but still not working
           Console.WriteLine("Hit any key to close"); Console.ReadKey(true);
       }
   }
}

@你说的“不起作用”是什么意思?它是否显示编译错误、抛出运行时异常或给出错误结果?谢谢。现在可以了。我使用了第一种解决方案。
found = false;
double priceFound = 0;
for (int i = 0; i < 5; i++)
{
    if (order.Equals(toppings[i]))
    {
        found = true;
        priceFound = price[i]; // store the found price
    }
}

Console.WriteLine("The price of " + order + " is $" + priceFound);
static void Main(string[] args)
{
   string[] toppings = { "cheese", "pepperoni", "green pepper", "onion", "mushroom" };
   double[] price = { 1.30, 1.90, 1.80, 1.70, 1.60 };

   Console.WriteLine("Choose a topping: cheese, pepperoni, green pepper, onion or mushroom");
   Console.WriteLine("Please type the topping that you want - exactly as it appears above:");
   string order = Console.ReadLine();
   Console.WriteLine("\nYou chose: " + order);

   int index = topping.IndexOf(order);

   if (index == -1)
   {
       Console.WriteLine("The product " + order + " was not found, please try again!!!!!!\n");
       Console.WriteLine("Hit any key to close"); Console.ReadKey(true);
       return;
   }

   Console.WriteLine("The price of " + order + " is $" + price[index]);
   Console.WriteLine("Hit any key to close"); 
   Console.ReadKey(true);
}