【问题标题】:Check if multidimensional matrix's last row contains vowels检查多维矩阵的最后一行是否包含元音
【发布时间】:2020-06-08 12:31:48
【问题描述】:

我正在尝试检查我的 8x10 字符矩阵的最后一行是否包含元音,并计算它们。问题是“.Contains”不起作用,如果不使用长“if”,我无法弄清楚如何解决这个问题。

class Program
{

    static void Main(string[] args)
    {
        var matrix = new char[8, 10];
        Random rnd = new Random();

        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                matrix[i, j] = (char)rnd.Next('a', 'z' + 1);
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine();
        }


        Console.ReadKey();
    }
}

}

【问题讨论】:

  • 您能否分享一些代码,以便我们知道您使用的是什么类型的数据结构?
  • 是的,请显示您的代码。这应该非常简单,就像这样LastArrayLine.Count(character =&gt; Vowels.Any(vowel =&gt; vowel == character))
  • 不确定。但是,如果您粘贴代码,我们可以以某种方式帮助您。如果您不想,只需看看 LINQ ......它可能会有所帮助

标签: c# arrays matrix multidimensional-array


【解决方案1】:

这段代码对我有用(没有 .Contains):

public class Program
{   
    public static char[] GetRow(char[,] matrix, int rowNumber)
    {
        return Enumerable.Range(0, matrix.GetLength(1))
                .Select(x => matrix[rowNumber, x])
                .ToArray();
    }

    public static void Main()
    {
        var array = new [,]
            {
                {'j', 'j', 'j', 'j', 'j'},
                {'j', 'j', 'j', 'j', 'j'},
                {'j', 'j', 'j', 'j', 'j'},
                {'a', 'j', 'j', 'j', 'j'},
            };
        char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
        var lastRowIndex = array.GetLength(0) - 1;
        var lastRow = GetRow(array, lastRowIndex);
        int vowelCount = lastRow.Count(x => vowels.Any(vowel => vowel == x));

        if(vowelCount == 0) Console.WriteLine("There is no vowel in matrix's last row");
        else if(vowelCount > 0) Console.WriteLine("There are vowel(s) in matrix's last row");       
    }
}

但是,下面的这段代码也适用于我,使用 .Contains() (我更喜欢第二个,因为它更简洁,性能更好):

public class Program
{   
    public static char[] GetRow(char[,] matrix, int rowNumber)
    {
        return Enumerable.Range(0, matrix.GetLength(1))
                .Select(x => matrix[rowNumber, x])
                .ToArray();
    }

    public static void Main()
    {
        var array = new [,]
            {
                {'j', 'j', 'j', 'j', 'j'},
                {'j', 'j', 'j', 'j', 'j'},
                {'j', 'j', 'j', 'j', 'j'},
                {'a', 'j', 'j', 'j', 'j'},
            };
        char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
        var lastRowIndex = array.GetLength(0) - 1;
        var lastRow = GetRow(array, lastRowIndex);
        int vowelCount = lastRow.Count(x => vowels.Contains(x));

        if(vowelCount == 0) Console.WriteLine("There is no vowel in matrix's last row");
        else if(vowelCount > 0) Console.WriteLine("There are vowel(s) in matrix's last row");       
    }
}

【讨论】:

    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2018-10-14
    • 1970-01-01
    相关资源
    最近更新 更多