关于c sharp中的数组的问题。 [英] Question about arrays in c sharp.

查看:137
本文介绍了关于c sharp中的数组的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我按照下面编写的方式运行我的程序,它将不会给我正确的输出,它将不会搜索我想要搜索的数字。但是如果我:

If I run my program like the way it is written below it won't give me the correct output,it won't search the number I want to search. But if I :

list[i] = Convert.ToInt32(Console.ReadLine());

而不是

instead of

Console.WriteLine("{0}", i);;



并输入我自己的数字,它会找到我想要找到的数字。我无法弄清楚为什么。有人可以帮帮我吗?




and input my own numbers it will find the numbers I want to find. I couldn't figure out why. Can anybody help me out?

{
    class program
    {
        public class BinarySearch
        {
            public static int Search(int[] list, int x, int lower, int upper)
            {
                if (lower <= upper)
                {
                    int middle = (lower + upper) / 2;
                    if (x == list[middle])
                        return middle;
                    else if (x < list[middle])
                        return Search(list, x, lower, middle - 1);
                    else
                        return Search(list, x, middle + 1, upper);
                }
                return 0;
            }
                  public static void Main(String[] args)
                   {
                       int key;       // the search key
                       int index;     // the index returned
                       int[] list = new int[100];
                       Console.WriteLine("\nEnter arrays of 10 set numbers (strike enter after each set of numbers\n");
                       for (int i = 1; i <= 5; i++)
                           Console.WriteLine("{0}", i);
                    //   {
                      //     list[i] = Convert.ToInt32(Console.ReadLine()); 
                       //}
                       Console.WriteLine("...................................................\n");
                       Console.WriteLine("\nEnter the number to be searched in the list.");

                       key = Convert.ToInt32(Console.ReadLine());
                       index = Search(list, key, 0, 5);
                       Console.WriteLine("...................................................\n");
                       if (index == 0)
                           Console.WriteLine("Key {0} not found", key);
                       else
                           Console.WriteLine("Key {0} found at index {1}", key, index);
                   }
               }
           }
       }

推荐答案

停下来想想你是什么正在做。

你显示的代码已经注释掉了set load - 所以数组永远不会被设置为一个值,所以你永远找不到零以外的任何值(<$ c的默认值) $ c> int )



当你确实有这个代码时,它不会填充数组 - 它从中读取五个数字用户并使用它们填充您已分配的100个中的前五个位置。并且你提示用户也是坏的:

Stop and think about what you are doing.
The code you show has the set load commented out - so the array is never set to a value, so you can never find any value other than zero (the default for an int)

And when you do have that code in, it doesn't fill the array - it reads five numbers from the user and uses them to fill the first five locations in the 100 you have allocated. And you prompt to the user is bad as well:
Console.WriteLine("\nEnter arrays of 10 set numbers (strike enter after each set of numbers\n");

因为我读这条信息的方式是我应该输入:

Because the way I would read that message is that I should enter:

1,2,3,4,5,6,7,8,9,10[ENTER]

那个会让你的程序崩溃。



更糟糕的是 - 你的方法只有在我输入数字作为排序列表时才会起作用 - 如果我输入然后未分类或反转:

And that will make your program crash.

And worse - your method would only work if I enter the numbers as a sorted list - if I enter then unsorted or reversed:

10,9,8,7,6,5,4,3,2,1

那么你的代码只能在中间找到值。



说真的:想想你已经分配的任务,并在纸上计算出你将如何手动完成它。因为此刻,你似乎只是试图猜测什么有效,这是很多麻烦和浪费工作的秘诀!

Then your code only find values exactly in the middle.

Seriously: think about the task you have been assigned, and work out on paper how you would do it manually. Because at the moment, you just seem to be trying to guess what works, and that is a recipe for a lot of trouble and wasted work!


这篇关于关于c sharp中的数组的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆