数组比较,学校任务不起作用

时间:2016-08-15 18:56:27

标签: c# arrays compare

首先在这里发帖,告诉我,如果我做错了什么:)

所以我有这段代码:

    static void Main(string[] args)
    {
        //declaring first array
        Console.Write("Enter the size of the first the array: ");
        int sizeOne = Int32.Parse(Console.ReadLine());
        int[]firstArray = new int[sizeOne];

        //fill it from console
        for (int counter = 0; counter<=sizeOne - 1; counter++)
        {
            Console.Write("Please enter a value: ");
            firstArray[counter] = Int32.Parse(Console.ReadLine());
            //for test
            // Console.WriteLine(firstArray[counter]);
        }

        //declaring second array
        Console.Write("Enter the size of the second array: ");
        int sizeTwo = Int32.Parse(Console.ReadLine());
        int[] secondArray = new int[sizeTwo];

        //fill it from console
        for (int counter = 0; counter <= sizeTwo - 1; counter++)
        {
            Console.Write("Please enter a value: ");
            firstArray[counter] = Int32.Parse(Console.ReadLine());
            //for test
            // Console.WriteLine(secondArray[counter]);
        }

        //compare size and values, as sidenote it could write not equal even 
        //when the user inputs two arrays with different lengths to save time :)
        if (firstArray.Length == secondArray.Length)
        {
            for (int counter = 0; counter <= sizeOne; counter++)
            {
                if ((firstArray[counter]) != (secondArray[counter]))
                {
                    Console.WriteLine("The two arrays aren't equal");
                    break;
                }
                else
                {
                    Console.WriteLine("The two arrays are equal");                        
                }
            }
        }
        else
        {
            Console.WriteLine("The two arrays aren't equal");
        }
    }

它应该按长度和元素比较数组。如果两个数组具有不同的长度但是在相同数量的元素上它总是写入不相等。我错过了什么?

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

这是一个错字或复制粘贴错误,无论你想要什么叫它。在第二个循环中,当您应该填充secondArray时,您错误地填充firstArray。这意味着secondArray只有零。您可能幸运(或不幸)firstArray总是等于或大于secondArray。否则你会得到一个例外,这可能有助于你发现你的错误。

请注意,一旦修复它,您还将获得一个越界异常,因为您的比较循环使用counter <= sizeOne条件,这是错误的。它应该是counter < sizeOne,否则你将超过数组的末尾。

答案 1 :(得分:1)

稍微修改一下循环: 您需要一个布尔标志,指示是否存在不匹配。否则它将打印&#34;两个数组相等&#34;如果只是第一个元素匹配。

if (firstArray.Length == secondArray.Length)
{
    bool areEqual = true;
    for (int counter = 0; counter < firstArray.Length; counter++)
    {
        if ((firstArray[counter]) != (secondArray[counter]))
        {
            areEqual = false;
            //Console.WriteLine("The two arrays aren't equal");
            break;
        }
        // This would get executed if the first elements are equal, but others are not
        //else
        //{
        //  Console.WriteLine("The two arrays are equal");                        
        //}
    }

    if (areEqual)
       Console.WriteLine("The two arrays are equal"); 
    else 
       Console.WriteLine("The two arrays aren't equal");
}

然后当然有一个名为SequenceEqual的内置函数(在.NET中)可以比较两个数组:

using System.Linq;    
...
bool areEqual = firstArray.SequenceEqual(secondArray);
if (areEqual)
   Console.WriteLine("The two arrays are equal"); 
else 
   Console.WriteLine("The two arrays aren't equal");

See NetFiddle

还有一个错字:你已经两次填充firstArray了。

    //fill it from console
    for (int counter = 0; counter <= sizeTwo - 1; counter++)
    {
        Console.Write("Please enter a value: ");
        // **Must be secondArray**
        firstArray[counter] = Int32.Parse(Console.ReadLine());
        //for test
        // Console.WriteLine(secondArray[counter]);
    }