2016-10-15 65 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

非常感謝你!這工作! –