Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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#_Loops_Infinite Loop_Binary Search - Fatal编程技术网

C#二叉树循环故障

C#二叉树循环故障,c#,loops,infinite-loop,binary-search,C#,Loops,Infinite Loop,Binary Search,我正在做一个循环,从数组中搜索一个值。如果我找到了,职位就会被打印出来,如果没有,就不会打印“没有这样的号码”。 但是,如果我有几个相同的数字,我只会收到一个,然后我就结束了循环。如果数组中的数字相同,如何获取控件? 我的最佳尝试以无休止的循环或从一开始就完成的循环结束 static public void Searching(int[] arr) { string x = "yes"; Console.WriteLine("Enter please searching num

我正在做一个循环,从数组中搜索一个值。如果我找到了,职位就会被打印出来,如果没有,就不会打印“没有这样的号码”。 但是,如果我有几个相同的数字,我只会收到一个,然后我就结束了循环。如果数组中的数字相同,如何获取控件? 我的最佳尝试以无休止的循环或从一开始就完成的循环结束

static public void Searching(int[] arr)
{
    string x = "yes";

    Console.WriteLine("Enter please searching number");
    while (x == "yes")
    {
        bool found = false;
        int target = Convert.ToInt32(Console.ReadLine());
        int searchkey = target;
        int mid = 0, first = 0, last = arr.Length - 1;
        while (!found && first <= last)
        {
            mid = (first + last) / 2;

            if (target == arr[mid])
                found = true;


            else
            {
                if (target > arr[mid])
                {
                    first = mid + 1;
                }

            if (target < arr[mid])
            {
                last = mid - 1;
            }
        }
    }
    String foundmsg = found
                ? "Item " + searchkey + " was found at position " + mid
                : "Item " + searchkey + " was not found";
    Console.WriteLine(foundmsg);
    Console.WriteLine("would you like to find another number?");
    x = Console.ReadLine();

        }

    }
静态公共无效搜索(int[]arr)
{
字符串x=“是”;
Console.WriteLine(“请输入搜索号码”);
而(x=“是”)
{
bool-found=false;
int target=Convert.ToInt32(Console.ReadLine());
int searchkey=target;
int mid=0,first=0,last=arr.Length-1;
而(!已找到和第一个arr[mid])
{
第一个=中间+1;
}
如果(目标
如果要返回数组中找到特定值的所有位置,请创建一个列表(如
list
),在数组中找到数字后,将该位置添加到列表中。这样,您就可以搜索整个阵列,而不会中断循环

执行完毕后,列表中将有一个或多个项目,或者列表将为空。如果列表中有项目,则显示这些项目。如果列表中没有项目,则返回“未找到”消息

比如说,

var indexes = new List<int>();
for(var index = 0; index < source.Length; index++)
{
    if(source[index] == target) indexes.Add(index);
}
List<int> GetIndexesOfMatchingNumbers(int[] source, int target)
{
    var indexes = new List<int>();
    for(var index = 0; index < source.Length; index++)
    {
        if(source[index] == target) indexes.Add(index);
    }
}
var索引=新列表();
对于(var index=0;index
完成后,您将有一个匹配列表或一个空列表

许多嵌套循环可能会令人困惑。您有一个处理控制台输入的外部循环。使其更易于阅读的一种方法是将零件隔离到函数中。比如说,

var indexes = new List<int>();
for(var index = 0; index < source.Length; index++)
{
    if(source[index] == target) indexes.Add(index);
}
List<int> GetIndexesOfMatchingNumbers(int[] source, int target)
{
    var indexes = new List<int>();
    for(var index = 0; index < source.Length; index++)
    {
        if(source[index] == target) indexes.Add(index);
    }
}
列出GetIndexeSofMatchingNumber(int[]源,int目标)
{
var索引=新列表();
对于(var index=0;index
您可以从“main”函数调用此函数。它在功能上完全相同,但它减少了阅读代码时必须遵循的逻辑量。这使得它更容易理解,即使是对写它的人来说也是如此。

不要重复这个。C#内置了一些功能来帮助您

static public void Searching(int[] arr)
    {
        string x = "yes";

        Console.WriteLine("Please enter a number for which to search...");
        int target = Convert.ToInt32(Console.ReadLine());

        string Response = "Result of search is:"
        +  arr.IndexOf(target) != -1 
        ? arr.IndexOf(target).ToString() : "Not Found";
       //indexOf finds the first index of obj. in array
        Console.WriteLine(Response);
     }
这应该满足您的“当我找到一个倍数时,我只收到一个”要求。你总是第一次出现

如果您需要所有实例,并使用循环,则没有什么不同

static public void Searching(int[] arr)
    {
        string x = "yes";

        Console.WriteLine("Please enter a number for which to search...");
        int target = Convert.ToInt32(Console.ReadLine());
        string result = string.Empty
        for(int i =0; i < arr.Length; i++)
              if (i = target)
              { result += i.ToString() + ", " }
        }
        if(result != string.Empty)
        {
           result = result.SubString(0, result.Length - 2);
           //removes extra space and colon
        } else {
           result = "Not Found";
        }
        Console.WriteLine("Result of search is:"
        + result);
     }
静态公共无效搜索(int[]arr)
{
字符串x=“是”;
Console.WriteLine(“请输入要搜索的号码…”);
int target=Convert.ToInt32(Console.ReadLine());
字符串结果=string.Empty
对于(int i=0;i
您可以尝试使用
Array.BinarySearch
查找某个项目的索引,然后从索引开始前后移动:

int[]source=new[]{1,2,3,4,4,4,4,5,15,20};
int searchkey=4;
int first=-1;
int last=-1;
int index=Array.BinarySearch(源,searchkey);
如果(索引>=0){
第一个=索引;
last=索引;
对于(int i=index-1;i>=0;--i)
if(源[i]!=toFind)
打破
其他的
第一个=i;
for(inti=index+1;i
我猜英语不是你的第一语言。我真的不知道你为什么要用循环。如果您将数字数组转换为字符串,然后使用
.IndexOf(inputNumber)
,您将得到所需的内容。我是否遗漏了语言障碍方面的内容?你可能想改写你的第一句话,因为很多人觉得这个词令人反感。我刚刚编辑了它(还做了代码格式化等),但现在还有一个待编辑的问题。我的编辑将退出窗口--@cdove不,这很好,我知道我可以使用该函数,但这是我的类任务,所以我应该只使用循环。乍一看,你的代码对我来说还行。你能包含导致你得到无限循环的输入吗?编辑:你是说如果你多次拥有同一个号码,你想输出每个匹配号码的位置吗?我相信1)是为了学校,2)他正在尝试进行二进制搜索。IndexOf不是二进制搜索。IndexOf查找第一个实例。他说数字可能会在数组中出现多次,他需要返回每个数字的位置。但这并不满足要求(隐藏在注释中):“但这是我的课堂任务,所以我应该只使用循环”是的,在他添加那一部分之前,我正在写这个答案。根据经验,我将方法保持在15行或更少(不包括空格或只带大括号的行)。无论我们是新手还是有经验的人,阅读自己的代码还是别人的代码,这只会让大脑更容易处理。通常是