[已解决] - 关于方法的另一个C#问题 [英] [SOLVED] - Another C# question about Methods

查看:73
本文介绍了[已解决] - 关于方法的另一个C#问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候大家,我有另外一个问题如下:

CalculateStats:此方法必须计算最低,最高和平均销售额

金额。接收有效销售值数组作为方法参数,以及

无效值的计数。计算出的最低,最高和平均金额可通过out方法参数获得
。必须使用for循环进行计算,

但仅限于数组中的有效值。要计算最低,最高和平均值,可以不使用内置阵列方法。因此,您必须手动执行计算。此方法

不得包含任何Console语句。注意:不需要try / catch块。



我的问题是最低的是alwasy 0因为也计算了无效的条目,我如何只计算有效值?我已经处理过在以前的方法上处理这些无效值。现在问题是每次我运行程序,最低总是== 0



这是我的代码:



Greetings everyone, I have another question as follows:
CalculateStats: this method must calculate the lowest, highest and average sales
amount. The array of valid sales values is received as a method parameter, as well as, the
count of invalid values. The calculated lowest, highest and average amounts are made
available via out method parameters. A for loop must be used to do the calculations,
but only on the valid values in the array. To calculate the lowest, highest and average values, no built-inarray methods may be used. Thus, you must manually do the calculations. This method
must not contain any Console statements. Note: No try/catch blocks are required.

My problem is that the lowest is alwasy 0 because an invalid entry is also counted, how do I calculate only valid values? I have already dealt with handling these invalid values on a previous method. The problem now is everytime I run the program, lowest is always ==0

Here is my code:

static void CalculateStats( decimal[] salesAmount,out decimal lowest, out decimal highest,int invalidEntries,out decimal average)
        {

             lowest = salesAmount[0];
             highest = salesAmount[0];
             average = 0;
             decimal totalSalesAmount = 0;


            for (int k = 0; k < salesAmount.Length; k++)
            {
                 if (salesAmount[k] < lowest)
                 {
                        lowest = salesAmount[k];
                 }

                if (salesAmount[k] > highest)
                {
                    highest = salesAmount[k];

                }
                totalSalesAmount += salesAmount[k];
            }

            average = totalSalesAmount / (salesAmount.Length-invalidEntries);
        }

推荐答案

您尚未说明定义有效/无效值的内容。

实际上,问题陈述有点自相矛盾,因为数组被声明为包含有效销售值,但是您被指示在计算中仅使用数组的有效值。

如果您知道有效值或无效值的特征是什么,那么您可以在中为循环检查它:

You haven''t stated what defines a valid/invalid value.
In fact, the problem statement is a bit self-inconsistant in that the array is stated as containing "valid sales values", but you are instructed to use only the valid values of the array in the calculations.
If you know what the characteristic of an valid or invalid value is, then you can check for it inside the for loop:
for (int k = 0; k < salesAmount.Length; k++)
{
  decimal amount = salesAmount[k];  // no need to repeat the indexing
  if (/* check if amount is valid */)
  {
    if (amount < lowest)
    {
        lowest = amount;
    }
 
    if (amount > highest)
    {
        highest = amount;
    }
    totalSalesAmount += amount;
  }
}



如果您需要确定哪些是有效的,那么提供无效值的计数似乎很不寻常。提供该值似乎毫无意义,因为您可以在循环中自行确定它。


It seems unusual to provide the count of invalid values if you are expecte to determine which are valid yourself. There seems no point in providing that value, as you can determine it yourself in the loop.


您拥有的信息并不精彩 - 它并没有告诉您如何识别无效的值,因此可以安全地假设它们全部在开头或结尾 - 后者最有可能。

所以你要做的就是改变你的循环终止条件:

The info you have isn''t brilliant - it doesn''t tell you how to identify invalid values, so it is safe to assume that they are either all at the beginning or at the end - the latter is the most likely.
So all you have to do is change your loop termination condition from:
k < salesAmount.Length

to:

to:

k < salesAmount.Length - invalidEntries

并忽略无效条目


这是我的猜测。



输入salesAmount数组是从textBox中读取的,其中用户类型sales的分隔值为''semicoln''。



即字符串salesAmountString =2; 12; r; 45;你好; 874.54; 200; g; K; 23;



所以这就是你要做的事情。

here is my guess.

the input salesAmount array is read from a textBox where user type sales with ''semicoln'' seperated value.

i.e string salesAmountString = "2; 12; r; 45; hello; 874.54; 200; g; K; 23";

so this is what you have to do.
some method()
{
 string salesAmountString = "2; 12; r; 45; hello; 874.54; 200; g; K; 23";
 string []splitValue = salesAmountString.Split(';');

 List<decimal> salesList = new List<decimal>();

 foreach (string str in splitValue)
 {
     decimal parseValue;
     if (decimal.TryParse(str, out parseValue))
     {
       salesList.Add(parseValue);
       }
 }

 CalculateStats(salesList.ToArray(), out lowest, out highest, out average);
}

void CalculateStats(decimal[] salesAmount, out decimal lowest, out decimal highest, out decimal average)
{
 lowest = salesAmount[0];
 highest = salesAmount[0];
 average = 0;
 decimal totalSalesAmount = 0;

  for (int k = 0; k < salesAmount.Length; k++)
  {
    decimal amount = salesAmount[k];
 
    if (amount < lowest)
    {
       lowest = amount;
    }

    if (amount > highest)
    {
       highest = amount;
    }
      totalSalesAmount += amount;
  }
  average = totalSalesAmount / (salesAmount.Length);
}


这篇关于[已解决] - 关于方法的另一个C#问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆