Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数组的最大值函数_C# - Fatal编程技术网

C# 数组的最大值函数

C# 数组的最大值函数,c#,C#,我读入了一个文件,将该文件按排序为三列,并将每列中的所有数据放入3个数组中。现在我需要创建一个方法,从第二列数组中获取最大值。代码如下: static void Main(string[] args) { string file1 = System.IO.File.ReadAllText(@"C:\file1.txt"); List<double> Array1 = new List<double>(); List<double>

我读入了一个文件,将该文件按排序为三列,并将每列中的所有数据放入3个数组中。现在我需要创建一个方法,从第二列数组中获取最大值。代码如下:

static void Main(string[] args) 
{ 
    string file1 = System.IO.File.ReadAllText(@"C:\file1.txt");

    List<double> Array1 = new List<double>();
    List<double> Array2 = new List<double>();
    List<double> Array3 = new List<double>();

    IEnumerable<string> lines = File.ReadLines(@"C:\File1.txt");

    foreach (string line in lines)
    {
        string[] columns = line.Split(',');

        if (columns.Length != 3)
        {
            continue; // skips this line
        }

        Array1.Add(Convert.ToDouble(columns[0]));
        Array2.Add(Convert.ToDouble(columns[1]));
        Array3.Add(Convert.ToDouble(columns[2]));
    }

Console.WriteLine(Max(Array1.ToArray()));
}
static double Max (double[] x)
{
    double maxValue = x.Max();
    return maxValue;
}
static void Main(字符串[]args)
{ 
字符串file1=System.IO.File.ReadAllText(@“C:\file1.txt”);
列表数组1=新列表();
List Array2=新列表();
列表数组3=新列表();
IEnumerable lines=File.ReadLines(@“C:\File1.txt”);
foreach(行中的字符串行)
{
string[]columns=line.Split(',');
如果(columns.Length!=3)
{
continue;//跳过这一行
}
Array1.Add(Convert.ToDouble(列[0]);
Array2.Add(Convert.ToDouble(第[1]列));
Array3.Add(Convert.ToDouble(第[2]列));
}
Console.WriteLine(最大值(Array1.ToArray());
}
静态双最大值(双[]x)
{
double maxValue=x.Max();
返回最大值;
}
错误:应为类型或命名空间定义或文件结尾

我需要修正Max方法,而且我不知道如何说我们需要得到的最大值是数组2。然后我需要返回main并使用Max方法获得数组2的最大值。很明显,它们是Max函数中的初学者错误,但我已经研究了这个问题,我不确定如何继续

static double Max (double[] x)
{
    double[] x // This line shouldn't be here

    double maxValue = x.Max();
    return maxValue;
}
您在参数和方法中声明了两次
double[]x
。这会把事情搞砸的

另外,尽量不要让方法的名称与API中的名称相同,只是为了使代码更易于阅读

最后,您从未从main调用过该方法:P

namespace Your_Name_Space
{
    class Your_Program
    {
        public static void Main(string[] args)
        {
            string file1 = System.IO.File.ReadAllText(@"D:\file1.txt");

            List<double> Array1 = new List<double>();
            List<double> Array2 = new List<double>();
            List<double> Array3 = new List<double>();

            IEnumerable<string> lines = File.ReadLines(@"D:\File1.txt");

            foreach (string line in lines)
            {
                string[] columns = line.Split(',');

                if (columns.Length != 3)
                {
                    continue; // skips this line
                }

                Array1.Add(Convert.ToDouble(columns[0]));
                Array2.Add(Convert.ToDouble(columns[1]));
                Array3.Add(Convert.ToDouble(columns[2]));
            }

            Console.WriteLine(Max(Array1.ToArray()));
            Console.ReadKey();
        }

        public static double Max(double[] x)
        {
            double maxValue = x.Max();
            return maxValue;
        }
    }
}
你所有的代码都运行了。当编译并执行上述代码时,会产生以下结果:

7

“预期文件结尾”错误的原因是大括号过多。你应该控制你的牙套。值7不是第二列

谢谢你,这很有效。同样,我在主体中写道:“Max(Array1)和console.writeline(Max(Array1));”这里怎么了?很高兴听到;D-您能显示堆栈跟踪或告诉我发生了什么/没有发生什么吗?
console.writeline
出错。编码区分大小写。这样做
Console.Writeline(Max(array1))
Console.WriteLine(最大(数组1))
如果我调用函数并在其中放入Array1,那么它就是main,我以为这会得到数组的最大值,但它返回一个错误:max(Array1);我觉得这个代码不是你的。在进入高级编程之前,请先阅读一些关于c#的教程。
7