在C#中搜索数组元素时遇到代码问题

时间:2016-05-21 18:51:47

标签: c# arrays visual-studio find element

我一直在努力解决这段代码,我似乎无法找出我出错的地方。基本上我想使用Integer搜索数组,如果它匹配该数组中的元素,则返回bool变量为true。这是非常自我解释但不能为我的生活弄清楚!有什么想法吗?

这是代码;

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

namespace ArrayProject
{
    class ArrayProgram
    {
        public bool ElementAt(int[] intArray, int valueToBeFound)
        {
            bool intAt = false;
            int numberTofind;

            Console.WriteLine("Please enter the number you wish to search for within the array: ");
            numberTofind = Convert.ToInt32(Console.ReadLine());


            foreach (int x in intArray)
            {
                if (x == numberTofind)
                {
                    intAt = true;
                }
                else
                {
                    intAt = false;
                }
            }
            if (intAt == true)
            {
                Console.WriteLine("{0} is in the array!", numberTofind);
            }
            else
            {
                Console.WriteLine("{0} is not in the array.", numberTofind);
            }

            return intAt;
        }
        public void RunProgram()
        {
            int[] intArray = { 20, 30, 40, 50, 60, 50, 40, 30, 20, 10 };
            int numberTofind = 0;

            ElementAt(intArray, numberTofind);

        }  // end RunProgram()

        static void Main(string[] args)
        {
            ArrayProgram myArrayProgram = new ArrayProgram();
            myArrayProgram.RunProgram();

            Console.WriteLine("\n\n===============================");
            Console.WriteLine("ArrayProgram: Press any key to finish");
            Console.ReadKey();
        }
    }
}

4 个答案:

答案 0 :(得分:1)

        int[] intArray = { 20, 30, 40, 50, 60, 50, 40, 30, 20, 10 };
        int numberToFind = 0;

        //variant 1 (using System.Linq):
        bool bInside1 = intArray.Contains(numberToFind);

        //variant2
        bool bInside2 = Array.IndexOf(intArray, numberToFind) >= 0;

如果你想编写自己的函数:

    bool IsInside(int[] arrToSearch, int nToSearch)
    {
        foreach (int n in arrToSearch)
        {
            if (n == nToSearch)
                return true;
        }
        return false; //not found
    }

答案 1 :(得分:1)

问题是你的循环会继续检查元素并更新intAt,即使它找到了你正在寻找的元素。

如果数组为{1, 2, 3, 4}并且您的代码正在搜索1,则首先会检查索引0。这是匹配,因此intAt变为true。接下来它将尝试索引1。这个不匹配,因此它将intAt设置为false。然后它将尝试索引23等,从未找到匹配。

答案 2 :(得分:1)

Linq可以很容易地做到这一点。

using System.Linq;

public static string test(int[] numberArray, int find) 
{ 
            bool s = false;
            numberArray.ToList().ForEach(x => { if (x == find) s = true; }); 
            return s ? "It contains it." : "Can't find it.";
}

然而,有一种方法。你可以使用.Contains和一个数组作为一个人在我上​​面说。

答案 3 :(得分:0)

如果您想保留您的实施,请尝试以下方法:

class ArrayProgram
{
    public bool ElementAt(int[] intArray, int valueToBeFound)
    {
        foreach (int x in intArray)     
            if (x == valueToBeFound)        // if you found your value in the array
                return true;                // you return true

        return false;   // otherwise, by this point the foreach has looped through all the elements and hasn't once entered in the above if (so it hasn't found your value) = you return false
    }

    public void RunProgram()
    {
        int[] intArray = { 20, 30, 40, 50, 60, 50, 40, 30, 20, 10,99 };
        int numberTofind;

        // I noticed that you're not using the numberTofind value either, so:

        Console.Write("Please enter the number you wish to search for within the array: ");
        numberTofind = Convert.ToInt32(Console.ReadLine());

        // and since you made a function that returns true if your value has been found, you might as well use it like this

        if(ElementAt(intArray, numberTofind)) // if the function returns true
            Console.WriteLine("{0} is in the array!", numberTofind);
        else
            Console.WriteLine("{0} is not in the array.", numberTofind);
    }  // end RunProgram()

    static void Main(string[] args)
    {
        ArrayProgram myArrayProgram = new ArrayProgram();
        myArrayProgram.RunProgram();

        Console.WriteLine("\n\n===============================");
        Console.WriteLine("ArrayProgram: Press any key to finish");
        Console.ReadKey();
    }
}