比较两个数组的方法,找到最大数字并打印具有最大数字的数组

时间:2017-01-12 19:58:42

标签: c# arrays

如果我有两个类型为double的数组

double [] a1 = { 15, 7, 8 };
double [] a2 = { 10, 5 };

我想创建一个方法,将这些作为参数,比较它们并打印出包含最大数字的完整数组?像这样:

double[] result = HasLargestElement(a1,a2);
foreach (double x in result)
{
    Console.WriteLine(x);
}

我试图以更简单的方式学习如何实现这一点,然后我将在我的项目中实现,但我不知道应该如何做到这一点..我是一个真正的初学者用c#所以把它带进去记住,刚刚开始时没有多少经验。

更新

代码现在看起来像这样:

class Largest
{
    double[] a1 = { 15, 7, 8 };
    double[] a2 = { 10, 5, 14, 3 };


    static void Main(string[] args) {
    public double[] LargestElement(double[] a1, double[] a2)
    {
        var v1 = a1.Max();
        var v2 = a2.Max();

        if (v1 > v2)
            return v1;
        return v2;
    }

    public double HasLargestElement(double[] a1, double[] a2)
    {
        return a1.Max() > a2.Max() ? a1 : a2;
    }
}

但我收到编译错误:

  

无法将类型'double []'隐式转换为'double'

有人看到了问题吗?如上所述我是2天前开始的。如果有人可以编辑我的代码,那将是惊人的,我只是想看看它如何正常工作

5 个答案:

答案 0 :(得分:3)

您可以使用Linq扩展方法来完成此任务。

using System.Linq;

var result = a1.Max() > a2.Max() ? a1 : a2;
foreach (var x in result)
    Console.WriteLine(x);

答案 1 :(得分:3)

这应该为你做

double [] a1 = {15, 7, 8};
double [] a2 = {10, 5};
var max = a1.Max() > a2.Max()?a1:a2;
Console.Write(string.Join(Environment.NewLine, max));

由于您希望有人修改您的代码,以便您可以在此处查看其工作原理:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    internal class Largest
    {
        private static double[] a1 = {15, 7, 8};
        private static double[] a2 = {10, 5, 14, 3};

        private static void Main(string[] args)
        {
            var arrayWithlargestElement = HasLargestElement(a1,a2);
            Console.Write(string.Join(Environment.NewLine, arrayWithlargestElement));
            Console.ReadKey();
        }

        public static double[] HasLargestElement(double[] a1, double[] a2)
        {
            return a1.Max() > a2.Max() ? a1 : a2;
        }
    }
}

答案 2 :(得分:0)

您需要的是'HasLargestElement'数组中的以下内容。

if (a1.Max() > a2.Max()){
     return a1;
}
else return a2;

question

下有更多信息

答案 3 :(得分:0)

这个怎么样?

public double LargestElement(double[] a1, double[] a2)
{
    var v1 = a1.Max();
    var v2 = a2.Max();

    if(v1>v2)
       return v1;
    return v2;
}

public double[] HasLargestElement(double[] a1, double[] a2)
{
    return a1.Max() > a2.Max() ? a1 : a2;
}

答案 4 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Largest
{
    double[] a1;
    double[] a2;

    public Largest()
    {
    }

    public void compareArray()
    {

        if (a1.Max() > a2.Max())
        {
            foreach (var x in a1)
            {
                Console.WriteLine(x.ToString());
            }
            Console.ReadKey();
        }
        else
        {
            foreach (var x in a2)
            {
                Console.WriteLine(x.ToString());
            }
            Console.ReadKey();
        }
    }

    static void Main(string[] args)
    {
        Largest compare = new Largest();
        compare.a1 = new double[5] { 14, 7, 3, 7, 1 };
        compare.a2 = new double[5] { 13, 24, 3, 6, 11};
        compare.compareArray();
        Console.ReadKey();
    }
}

几乎完全新的代码,让它工作。