2013-02-04 105 views
-2

我正在製作一個應用程序,它會給我用戶插入的數字的平均值,中等和範圍。
但我似乎無法添加數字,然後將它們除以二。
這是我嘗試:從arraylist中添加數字

public static String Find_Mean() 
    { 
     int Number = 0; 
     for (int size = 0; size < list.Count; size++) 
     { 
      Number = Convert.ToInt16(list[size].ToString()); 
      Number += Number; 
     } 
     int Final_Number = Number/2; 
     return Convert.ToString(Final_Number); 
    } 

我想要做的就是從數組列表一起添加所有的號碼,然後通過2

+2

爲什麼在使用int時使用字符串?改爲使用'List '! – abatishchev

+3

這需要改進:'Convert.ToInt16(list [size] .ToString())'。看起來好像你會使用'string'作爲所有的基本類型。 –

回答

1

您重新分配的號碼這裏的值:

for (int size = 0; size < list.Count; size++) 
{ 
    Number = Convert.ToInt16(list[size].ToString()); 
    Number += Number; 
}  

試試這個:

for (int size = 0; size < list.Count; size++) 
{ 
    Number += Convert.ToInt16(list[size].ToString()); 
}  
3

分化他們嘗試使用LINQ:

int[] x; 
x.Average(); 
x.Max(); 
x.Min(); 
+1

我同意你的看法,但它可能是作業:) –

1

每你在設置Number到循環中的數組列表元素並覆蓋你的總數,這就是爲什麼你沒有得到總數。您需要使用單獨的變量來保持總計。喜歡的東西:

int Number = 0; 
int Total = 0; 
for (int size = 0; size < list.Count; size++) 
{ 
    Number = Convert.ToInt16(list[size].ToString()); 
    Total += Number; 
} 
int Final_Number = Total/2; 

如果您正在使用NET 2.0或更高那麼它的更好,如果你可以使用一個generic列表List<int>

您也可以在迴路中改變你的轉換號碼:

Number = Convert.ToInt32(list[0]); 

由於Convert.ToInt32有對象類型的過載爲好,另外如果您的號碼是int型的,那麼它是Int32Int16

3
Number = Convert.ToInt32(list[size].ToString()); 

您在此覆蓋每次迭代的數值。

0

這裏是我目前使用計算一些簡單的統計:

private void CalculateStatistics(IEnumerable<Double> valuesToAggregate) 
{ 
    // We need to iterate multiple times over the values, so it makes 
    // sense to create a list to improve performance. 
    var aggregateMe = valuesToAggregate.ToList(); 

    if (aggregateMe.Count > 0) 
    { 
     // To calculate the median, the simplest approach 
     // is to sort the list. 
     aggregateMe.Sort(); 

     // Cause we already sorted the list, 
     // the min value must be available within the first element. 
     Min = aggregateMe[0]; 

     // the max value must be available within the last element. 
     Max = aggregateMe[aggregateMe.Count - 1]; 

     // The average has really to be calculated, by another iteration run. 
     Mean = aggregateMe.Average(); 

     // Taking the median from a sorted list is easy. 
     var midpoint = (aggregateMe.Count - 1)/2; 
     Median = aggregateMe[midpoint]; 

     // If the list contains a even number of element, 
     // the median is the average of the two elements around the midpoint. 
     if (aggregateMe.Count % 2 == 0) 
      Median = (Median + aggregateMe[midpoint + 1])/2; 
    } 
    else 
    { 
     // There is no value available to calculate some statistic. 
     Min = Double.NaN; 
     Max = Double.NaN; 
     Mean = Double.NaN; 
     Median = Double.NaN; 
    } 
} 

請注意,您可能可以改進此特定實施,具體取決於您獲取的數據類型(字符串,雙打等等)以及它們是如何存儲的(它們是否已經作爲允許操作的列表進入,等等)?

0
public static String Find_Mean() 
{ 
    List<int> integerList = list.Select(q => int.Parse(q)).ToList(); 
    var result = integerList.Sum()/2; 
    return result.ToString(); 
}