印刷价格?

时间:2015-11-13 04:53:10

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

1 个答案:

答案 0 :(得分:-1)

当您搜索您的打顶时,您需要记住价格:

-1

但是,您不需要自己迭代阵列 使用Array.IndexOf可以更轻松地完成所有操作。此方法返回数组中项的索引,如果尚未找到项,则返回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); }

data.table