2016-10-15 28 views
1

我需要定义三个数组(大小,价格和额外)。然后我不得不问他们的大小和他们的浇头选择。比萨并行阵列

使用for循环和平行阵列技术,漫步尺寸数组,发现用户的输入相匹配的尺寸。使用循环的当前索引,在价格数组中查找该尺寸的价格,并向用户写出比萨将花费多少钱。

如果用户表示,他们确实需要额外的配菜,仍然使用相同的索引你的循环,查找有多少,在你的额外阵列的成本,并告诉用户的比萨饼的总成本。

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string[] sizes = { "S", "M", "L", "X" }; 
      double[] prices = { 6.99, 8.99, 12.50, 15.00 }; 
      double[] extra = { 1.00, 2.00, 3.25, 4.50 }; 
      string inputToppings; 
      string inputSize; 
       Console.Write("What size pizza would you like? Enter S, M, L, or X: "); 
       inputSize = Console.ReadLine(); 
       Console.Write("Would you like extra toppings? Enter Y or N: "); 
       inputToppings = Console.ReadLine(); 

      for (int i = 0; i < sizes.Length; i++) 
      { 
        if (sizes[i] == inputSize) 
       { 
        Console.WriteLine("You ordered a {0} pizza that costs {1:C}.", sizes[i], prices[i]); 
        break; 
       } 
      } 

      Console.ReadLine(); 
     } 
    } 
} 

我的问题是,我可以得到的比萨尺寸和价格正确的输入,但我不能工作语句来显示比萨尺寸价格在一个的WriteLine形式的浇头。我花了几个小时,我找不到工作方法。请帮助...

+3

我知道这可能是家庭作业,你必须这样做,但这不是一个好的设计。一个更好的方法是创建一个名为'Pizza'的类,它有3个属性:Size,Price和Extra。然后有一个'比萨[]' –

回答

0

试着改变你的 “for” 循环到:

for (int i = 0; i < sizes.Length; i++) 
{ 
    if (sizes[i] == inputSize) 
    { 
     var totalPrice = prices[i] + (inputToppings == "Y" ? extra[i] : 0); 
     Console.WriteLine("You ordered a {0} pizza that costs {1:C}.", sizes[i], totalPrice); 
     break; 
    } 
} 

“希望这有助于。

+0

非常感谢你!这工作! –