将参数从 List 传递到方法

Passing parameters from a List to a method

我正在解决我的第一个训练任务,但我正处于死胡同:我不知道如何将值从 List 传递到 Percentile 方法。我自己想不通。 List 从 txt 中获取值,然后将它们传递给方法进行不同的计算,例如计算百分位数。我不知道如何 link 一个 sheet 到一个百分位数。

using System;
using System.Collections.Generic;
using System.IO;

namespace Fromtxt
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var filePath = @"C:\Users\Saint\Desktop\text1.txt";
            var numbers = NumbersFromTxt(filePath); 


        }

             static List<int> NumbersFromTxt(string filePath)
        { 
            var streamReader = new StreamReader(filePath); 
            var results = new List<int>(); 
            string line; 

            while ((line = streamReader.ReadLine()) != null) 
            {
                var isParsed = int.TryParse(line, out var result);
                if (!isParsed)
                    throw new Exception("no");
                results.Add(result);
            }
            return results;
        }

        private static void Percentile(List<int> list, decimal [] arr)
        {
            list = new List<int>() { };
            {
                int n = arr.Length;
                for (int i = 0; i < n; i++)
                {
                    decimal count = 0;
                    for (int j = 0; j < n; j++)
                    {
                        if (list[i] > list[j])
                        {
                            count++;
                        }
                    }
                    decimal percent = (count * 90) / (n - 1);

                    Console.WriteLine("\nPercentile of stocks Apple for June "
                    + (i + 1) + " = " + percent.ToString("F2"));
                }
            }
        }
    }
}

如果我没看错,你几乎已经设置好了一切:你只需要在计算 numbers 之后调用你的 Percentile 方法,就像你所做的那样传递这些 numbers filePath 对应 NumbersFromTxtPercentile(numbers, arr)。 (实际上不知道 arr 是干什么用的)

剩下的唯一问题是您在 Percentile 方法中重新分配 list,这会使您的初始列表“消失”(list = new List<int>() { };)。考虑删除此行。

而且我认为您不需要 arr 参数,因为您仅使用它来获取长度。试试 list.Count.

如果您在 Main() 中只调用一次 NumbersFromTxt() 方法,并且想在其他方法中使用它,并且您不打算为它们创建另一个 class,最好的决定是使用此值创建一个字段。在您的代码中它将是

using System;
using System.Collections.Generic;
using System.IO;

namespace Fromtxt
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var filePath = @"C:\Users\Saint\Desktop\text1.txt";
            NumbersFromTxt(filePath); 
            Percentile();

        }

        static List<int> results;

        static void NumbersFromTxt(string filePath)
        { 
            var streamReader = new StreamReader(filePath); 
            results = new List<int>(); 
            string line; 

            while ((line = streamReader.ReadLine()) != null) 
            {
                var isParsed = int.TryParse(line, out var result);
                if (!isParsed)
                    throw new Exception("no");
                results.Add(result);
            }
        }

        private static void Percentile()
        {
            
            {
                int n = results.count;
                for (int i = 0; i < n; i++)
                {
                    decimal count = 0;
                    for (int j = 0; j < n; j++)
                    {
                        if (results[i] > results[j])
                        {
                            count++;
                        }
                    }
                    decimal percent = (count * 90) / (n - 1);

                    Console.WriteLine("\nPercentile of stocks Apple for June "
                    + (i + 1) + " = " + percent.ToString("F2"));
                }
            }
        }
    }
}