2012-11-15 291 views
0

我有一个数组,我需要在打印数组时输出$符号。我已经看到了ValueType,但它不适用于我的数组。请帮助我,该怎么办?将货币添加到数组元素

这里是我的代码提前

string[,] Pizza = new string[5, 5] 
     { 
      {"Name of Pizza \t", "Small",  "Medium",  "Large",  "XLarge"}, 
      {"Plain \t \t",   "8.80",  "12.80", "16.80", "20.80"}, 
      {"Hawaian \t",   "10.90",  "15.90",  "20.90",  "25.90"}, 
      {"Beefy \t \t",   "10.90",  "16.90",  "21.90",  "26.90"}, 
      {"Vegetarian \t",  "10.90",  "14.90",  "19.90",  "24.90"} 
     }; 

     //print the array 
     Console.WriteLine("what type of Pizza do you want?"); 
     for (int i = 0; i < Pizza.GetLength (0); i++) 
     { 
      Console.WriteLine(); 
      for (int j = 0; j < Pizza.GetLength (1); j++) 
       Console.Write(Pizza[i, j]+'\t'); 
     } 

感谢。

+0

我还没试过什么,我还是个初学者。我想得到的是输出 –

+0

中的每个数字之前的货币符号我不喜欢使用字符串数组来存储所有内容。首先,你将东西存储在一个通用的数据结构中,就好像它们是相同的东西,它们不是:比萨饼的名称和每个尺寸的价格清单。其次,您将价格存储为字符串,因此您已经对这些值是数字的事实失去了任何认识。十进制数据类型最适合存储价格,它提供了一些简单的机制来产生格式正确的字符串值。标准格式字符串:http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx –

+0

感谢您的提示,但这是讲师的任务要求。 :) –

回答

1

这会帮助你

string[,] Pizza = new string[5, 5] 
{ 
    {"Name of Pizza \t", "Small",  "Medium",  "Large",  "XLarge"}, 
    {"Plain \t \t",   "8.80",  "12.80", "16.80", "20.80"}, 
    {"Hawaian \t",   "10.90",  "15.90",  "20.90",  "25.90"}, 
    {"Beefy \t \t",   "10.90",  "16.90",  "21.90",  "26.90"}, 
    {"Vegetarian \t",  "10.90",  "14.90",  "19.90",  "24.90"} 
}; 

//print the array 
Console.WriteLine("what type of Pizza do you want?"); 
for (int i = 0; i < Pizza.GetLength (0); i++) 
{ 
    Console.WriteLine(); 

    for (int j = 0; j < Pizza.GetLength (1); j++) 

     if(j > 0 && i > 0){ 
      Console.Write("$" + Pizza[i, j] + '\t'); 
     } 
     else{ 
      Console.Write(Pizza[i, j] + '\t'); 
     } 
} 
+0

非常感谢你 –

0

改写。

我的问题解决方案背后的核心思想是我使用十进制数据类型来表示价格,并且我使用标准的“C”格式来输出适当的货币符号。

enum PizzaSize 
{ 
    Small, 
    Medium, 
    Large, 
    XLarge 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var pizzas = new[] { 
      new { 
       Name = "Plain", 
       Prices = new [] { 
        new { Size = PizzaSize.Small, Price = 8.80M }, 
        new { Size = PizzaSize.Medium, Price = 12.80M }, 
        new { Size = PizzaSize.Large, Price = 16.80M }, 
        new { Size = PizzaSize.XLarge, Price = 20.80M }, 
       }.ToDictionary(item => item.Size, item => item.Price) 
      }, 
      new { 
       Name = "Hawaian", 
       Prices = new [] { 
        new { Size = PizzaSize.Small, Price = 10.90M }, 
        new { Size = PizzaSize.Medium, Price = 15.90M }, 
        new { Size = PizzaSize.Large, Price = 20.90M }, 
        new { Size = PizzaSize.XLarge, Price = 25.90M }, 
       }.ToDictionary(item => item.Size, item => item.Price) 
      }, 
      new { 
       Name = "Beefy", 
       Prices = new [] { 
        new { Size = PizzaSize.Small, Price = 10.90M }, 
        new { Size = PizzaSize.Medium, Price = 16.90M }, 
        new { Size = PizzaSize.Large, Price = 21.90M }, 
        new { Size = PizzaSize.XLarge, Price = 26.90M }, 
       }.ToDictionary(item => item.Size, item => item.Price) 
      }, 
      new { 
       Name = "Vegetarian", 
       Prices = new [] { 
        new { Size = PizzaSize.Small, Price = 10.90M }, 
        new { Size = PizzaSize.Medium, Price = 14.90M }, 
        new { Size = PizzaSize.Large, Price = 19.90M }, 
        new { Size = PizzaSize.XLarge, Price = 24.90M }, 
       }.ToDictionary(item => item.Size, item => item.Price) 
      }, 
     }; 

     int maxNameWidth = pizzas.Max(item => item.Name.Length); 

     const string nameOfPizzaLabel = "Name of Pizza"; 

     if (maxNameWidth < nameOfPizzaLabel.Length) 
     { 
      maxNameWidth = nameOfPizzaLabel.Length; 
     } 

     Console.WriteLine("what type of Pizza do you want?"); 

     Console.WriteLine("{0} \t{1}\t{2}\t{3}\t{4}", 
      nameOfPizzaLabel.PadLeft(maxNameWidth), 
      PizzaSize.Small, 
      PizzaSize.Medium, 
      PizzaSize.Large, 
      PizzaSize.XLarge); 

     foreach (var pizza in pizzas) 
     { 
      Console.WriteLine("{0}:\t{1:C}\t{2:C}\t{3:C}\t{4:C}", 
       pizza.Name.PadLeft(maxNameWidth), 
       pizza.Prices[PizzaSize.Small], 
       pizza.Prices[PizzaSize.Medium], 
       pizza.Prices[PizzaSize.Large], 
       pizza.Prices[PizzaSize.XLarge]); 
     } 
    } 
} 
+0

我对这个复杂的答案表示歉意。如果你是初学者,可能很难理解。为了供您参考,以下是此答案中使用的一些工具/技术的列表:匿名类型,匿名数组和Linq(特别是ToDictionary和Max函数)。 –