如何向用户输出与数组中最大值相关的附加信息?

问题描述:

我试图让我的代码向用户输出销售量最大的酱料(售出的数量是基于用户输入的),酱料的名称和价格。我可以得到它显示最大的价值,但我不知道如何也输出名称和价格在同一个writeline。这是我到目前为止有:如何向用户输出与数组中最大值相关的附加信息?

class MainClass 
{ 
    public static void Main (string[] args) 
    { 
     string[] names; 
     names = new string[5]; 

     names [0] = "Eastern Carolina"; 
     names [1] = "Western Carolina"; 
     names [2] = "Kansas City"; 
     names [3] = "SC Mustard"; 
     names [4] = "Texas"; 

     int[] quantity; 
     quantity = new int[5]; 

     double[] prices; 
     prices = new double[5]; 

     for(int i=0; i < 5; i++) 
     { 
      Console.Write ("Enter how many bottles of " + names[i] + " sold: "); 
      quantity [i] = Convert.ToInt32(Console.ReadLine()); 

      Console.Write ("Enter the price of " + names[i] + ": "); 
      prices [i] = Convert.ToDouble (Console.ReadLine()); 

     } 

     for(int i=0; i < 5; i++) 
     { 
      Console.WriteLine (names[i] + " sauce $" + prices[i] + ", bottles sold: " + quantity [i]); 
     } 

     List<string> myList = new List<string>(); 
     for(int i=0; i < 5; i++) 
      myList.Add (names[i] + " " + prices[i] + " " + quantity[i]); 

     foreach (string name in myList) 
      Console.WriteLine (name); 


     if (quantity.Length > 0) 
     { 
      int large = quantity[0]; 
      for (int i = 0; i < quantity.Length; i++) 
      { 
       if (large < quantity[i]) 
       { 
        large = quantity[i]; 
        quantity[i] = large; 
       } 
      } 
      Console.WriteLine("The most bottles sold is {0}", large); 
     } 

既然你阵列之间的共同属性是索引,你应该“救”,在你的搜索。 所以不是

int large = quantity[0]; 

int large = 0; 

和索引这样

if (quantity[large] < quantity[i]) 
{ 
    large = i; 
} 

我不知道你为什么会数量再次设置,大到搜索,你不”不需要那样做。之后,您可以使用array[large]访问所有其他阵列。 您应该考虑使用具有属性名称,价格和数量的类“Sauce”而不是三个数组来保持您的代码结构。