是什么导致我的代码不向控制台显示特定消息?

问题描述 投票:-1回答:1

我真的很接近完成我输入的这个小程序,但是遇到一个问题,我的一部分代码没有显示在控制台上。

我让控制台询问用户他们要包含在数组中的数字数量,它还会询问用户每个数字,最后询问他们要使用的搜索方法(二进制,线性或气泡)。

我想在控制台上显示的部分,它将询问用户他们要寻找的号码,用户将在其中键入该号码,并显示该号码以及该编号存储在(索引)的位置。

namespace Searching_Algorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            String userChoice;
            int element, searchElement, index;

            Console.WriteLine("Hello! Welcome to my Searching and Sorting Algorithm");
            Console.WriteLine("What search/sort method would you like to do?");
            userChoice = Console.ReadLine();
            Console.WriteLine("How many numbers do you have?");

            element = Convert.ToInt32(Console.ReadLine());
            int[] array = new int[element];


for (int i = 0; i < element; i++)
{
    Console.WriteLine("Please enter a number: ");
    array[i] = Convert.ToInt32(Console.ReadLine());
}

if (userChoice == "Binary Search")
{
    Console.WriteLine("What number are you looking for? ");
    searchElement = Convert.ToInt32(Console.ReadLine());
    index = binarySearch(array, 0, element - 1, searchElement);
    if (index == -1)
        Console.WriteLine("The element you are looking for is not in the list.");
    else
        Console.WriteLine("The element you are looking for is at index " + index);
    //this part above is not being displayed for some reason and I've tried to fix it but
    //can't figure out why 

}

if (userChoice == "Linear Search")
{
    Console.WriteLine("What number are you looking for? ");
    searchElement = Convert.ToInt32(Console.ReadLine());
    index = linearSearch(array, searchElement);
    if (index == -1)
        Console.WriteLine("\nThe element you are looking for is not in the list.");
    else
        Console.WriteLine("\nThe element you are looking for is at index " + index);
    //this part above is not being displayed for some reason and I've tried to fix it but
    //can't figure out why                     
}

if (userChoice == "Bubble Sort")
{
    bubbleSort(array);
    Console.WriteLine("After sorting using bubble sort Array is: ");

    for (int i = 0; i < array.Length; i++)
    {
        Console.WriteLine(array[i]);
    }

}

static void bubbleSort(int[] arr)
{
    int n = arr.Length;
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - i - 1; j++)
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
}

static int linearSearch(int[] arr, int x)
{
    for (int i = 0; i < arr.Length; i++)
    {
        if (arr[i] == x)
            return i;
    }
    return -1;
}
static int binarySearch(int[] arr, int l,int r, int x)
{
    if (r >= l)
    {
        int mid = l + (r - l) / 2;
        if (arr[mid] == x)
            return mid;
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);
        return binarySearch(arr, mid + 1, r, x);
    } 
    return -1;
}

c# search console
1个回答
0
投票

如果您提供完全正确输入,这很好用。默认情况下,字符串比较区分大小写,即使是最轻微的错误,大小写不匹配或多余的空格,也可能导致比较错误。在这样的控制台程序中,最好为用户提供一组预定义的选项,如果做出了有效的选择,则继续进行。

int choice = 0;
do
{
    Console.WriteLine("Select a search method.");
    Console.WriteLine("1. Binary Search");
    Console.WriteLine("2. Linear Search");
    Console.WriteLine("3. Bubble Sort");
    Console.WriteLine("99. Exit");
    Console.Write("Enter your choice number: ");
    var input = Console.ReadLine();
    if (int.TryParse(input, out choice))
    {
        switch (choice)
        {
            case 99:
                break;
            case 1:
                // Call Binary Search
                break;
            case 2:
                // Call Linear Search
                break;
            case 3:
                // Call Bubble Sort
                break;
            default:
                Console.WriteLine("Please enter a valid choice.");
                break;
        }
    }
    else
    {
        Console.WriteLine("Please enter a valid choice.");
    }
} while (choice != 99);
© www.soinside.com 2019 - 2024. All rights reserved.