【问题标题】:A logical error with my code我的代码存在逻辑错误
【发布时间】:2009-01-16 20:43:21
【问题描述】:

我写了这段代码,它总是显示相同的结果,为什么? 代码是一种搜索方法。

using System;
using System.Collections.Generic;
using System.Text;

namespace CArraySe
{
    class Program
    {
        class CArray
        {
            private int[] arr;
            private int upper;
            private int numElements;
            private int compCount;

            public CArray(int size)
            {
                arr = new int[size];
                upper = size - 1;
                numElements = 0;
                compCount = 0;
            }

            public void Insert(int item)
            {
                arr[numElements] = item;
                numElements++;
            }

            public void DisplayElements()
            {
                for (int i = 0; i <= upper; i++)
                {
                    Console.Write(arr[i]);
                    if (i == upper)
                    {
                        Console.WriteLine();
                        continue;
                    }
                    Console.Write(", ");
                }
            }

            public void Clear()
            {
                for (int i = 0; i <= upper; i++)
                    arr[i] = 0;
                numElements = 0;
            }
            public bool SeqSearch(CArray n, int sValue)
            {
                for (int index = 0; index < n.upper; index++)
                {
                    if (arr[index] == sValue)

                        return true;
                }

                compCount++;
                return false;
            }
            public int binSearch(CArray n, int value)
            {
                int upperBound, lowerBound, mid;
                upperBound = n.upper; lowerBound = 0;

                while (lowerBound <= upperBound)
                {
                    mid = (upperBound + lowerBound) / 2;

                    if (arr[mid] == value)
                        return mid;

                    else if (value < arr[mid]) upperBound = mid - 1;

                    else lowerBound = mid + 1;
                }
                compCount++;
                return -1;
            }

            static void Main(string[] args)
            {
                CArray nums = new CArray(10);
                Random rnd = new Random(100);
                for (int i = 0; i < 10; i++)
                    nums.Insert((int)(rnd.NextDouble() * 100));

                Console.WriteLine();
                Console.Write("The Binary Search Result is: ");
                Console.WriteLine(nums.binSearch(nums, 500));
                Console.WriteLine(nums.compCount);
                nums.Clear();
                for (int i = 0; i < 10; i++)
                    nums.Insert((int)(rnd.NextDouble() * 100));

                Console.Write("The Sequential Search result is: ");
                Console.WriteLine(nums.SeqSearch(nums, 500));
                Console.WriteLine(nums.compCount);
           }
       }
    }
}

即使我更改了我要查找的号码,它也始终显示相同的结果。

输出是:

The Binary Search Result is: -1
1
The Sequential Search result is: False
2
Press any key to continue . . .

【问题讨论】:

  • 你应该尝试评论你的代码
  • 或者使用自动化单元测试代替 cmets...

标签: c# search


【解决方案1】:

我认为您正在搜索的值 (500) 未找到。尝试输出 nums 数组并验证您要查找的内容是否在数组中。

另外,一个搜索返回一个 int,另一个返回一个 bool。有什么具体原因吗?

编辑:另外,Binary Search 仅适用于排序列表。

【讨论】:

    【解决方案2】:

    当找不到数字时,您的方法 binSearch 返回“-1”。由于您使用随机值填充数组,因此您搜索的数字很有可能不会被找到。所以你总是得到“-1”。

    要测试您的 binSearch 方法,您应该使用已知值填充数组,然后搜索一些保证在数组中的值。

    【讨论】:

      【解决方案3】:

      第一个答案是正确的。此外,即使您使用的是随机数,程序的每次运行也会产生相同的随机数序列。如果你想很好地测试代码,你应该在每次运行程序时使用不同的数字作为种子。

      【讨论】:

        【解决方案4】:

        正如其他人已经提到的,在一般情况下,不能保证您要搜索的数字在随机生成的数字列表中。 在您发布的特定情况下,该数字将永远出现在列表中,因为您正在生成 0-100 范围内的随机数,然后尝试查找 500

        【讨论】:

          【解决方案5】:

          运行您提供的内容不会添加超过 100 的值。如果您将添加更改为:

                  for (int i = 0; i < 9; i++)
                      nums.Insert((int)(rnd.NextDouble() * 100));
                  nums.Insert(500);
          

          binSearch 返回 9,但 SeqSearch 返回 false,因为您的循环搜索是 index

                  nums.Insert(500);
                  for (int i = 0; i < 9; i++)
                      nums.Insert((int)(rnd.NextDouble() * 100));
          

          将返回 -1;对于 SeqSearch 也是如此。

          【讨论】:

            【解决方案6】:

            虽然这可能无法直接回答您的问题,但这里有一些观察结果会使您的代码难以理解和调试:

          • 您需要numElements 或upper 之一,而不是两者。在 Clear() 中,您只将 numElements 设置为 0,而您在循环中到处都使用 upper?
          • 二进制搜索仅适用于排序数组
          • 如果 SeqSearch 和 BinSearch 正在接收数组的实例,它们不应该是静态方法而不是实例方法吗?
          • 【讨论】:

            • 如果我使用静态错误 1 ​​非静态字段、方法或属性“CArraySe.Program.CArray.arr”需要对象引用
            • 错误 3 静态成员 'CArraySe.Program.CArray.SeqSearch(CArraySe.Program.CArray, int)' 无法通过实例引用访问;改为使用类型名称来限定它
            • 如果要将方法转换为静态方法,则应将其更改为不使用其中的任何实例成员。您同时使用了 n 和 arr,这使得它更难阅读。您可能应该实现一个公共 CArray.getElement(int index) 方法并使用它而不是 arr[index]。
            猜你喜欢
            • 1970-01-01
            • 2015-08-01
            • 1970-01-01
            • 2020-12-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多