输入和我的数组令我困惑

时间:2014-01-10 03:03:53

标签: c# arrays sorting

为了节省时间,我必须清楚地传递一些东西,我将发布我的整个代码。这是一个控制台应用程序我正在发送一个以C#开始的朋友...

问题是,当我从get go进入退出时,它会退出。但是,让我说我点击进入。然后我点击了一个阵列。命中5,或6或任何数字。给我阵列。让我们说我想对它进行排序。它排序。但是,如果我点击EXIT它说不匹配。我不明白。发生了什么???

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication405
{
class Program
{
    static void Main(string[] args)
    {
        // Instantiate the delegate.
        Del handler = DelegateMethod;
        string _a = "";
        constructor con = new constructor();
        bool control = true;
        while (control)
        {
            Console.WriteLine("Enter EXIT to end the program.");
            Console.WriteLine("Press enter for options");
            ConsoleKeyInfo key = Console.ReadKey();
            if (key.Key.Equals(ConsoleKey.Enter))
            {
                Console.WriteLine("Enter C for a constructor.");
                Console.WriteLine("Enter M for a method.");
                Console.WriteLine("Enter A for an array.");
                Console.WriteLine("Enter D for a delegate.");
            }
            _a = Console.ReadLine();
            switch (_a.ToUpper())
            {
                case "EXIT":
                    Console.WriteLine("Thank you for using AJ's program.");
                    control = false;
                    break;
                case "C":
                    Console.WriteLine(con.a);
                    Console.WriteLine("Would you like to test another scenario?");
                    _a = Console.ReadLine(); 
                    if (_a.ToUpper() == "Y")
                    {
                        continue; 
                    }
                    control = false;
                    break;
                case "M":
                    metroid();
                    break;
                case "A":
                    Array();
                    break;
                case "D":
                    // call the delegate
                    handler("This is how you call a delegate. Also, Pasta noodles taste like wontons!!! =)");
                    break;
                default:
                    Console.WriteLine("No match");
                    break;
            }
        }
    }
    public delegate void Del(string message);
    public static void DelegateMethod(string message)
    {
        Console.WriteLine(message);
    }
    public class constructor
    {
        public string a = "This a is a constructor!";
    }
    static public void metroid()
    {
        string b = "This is a method!";
        Console.WriteLine(b);
    }
    static public void Array()
    {
        int temp, k;
        string ssSize = "";
        try
        {
            Console.WriteLine("This is a random array. Please enter the size.");
            string sSize = Console.ReadLine();
            int arraySize = Convert.ToInt32(sSize);
            int[] size = new int[arraySize];
            Random rd = new Random();
            Console.WriteLine();
            for (int i = 0; i < arraySize; i++)
            {
                size[i] = rd.Next(arraySize);
                Console.WriteLine(size[i].ToString());
            }
            Console.WriteLine("Would you like to sort this array?");
            ssSize = Console.ReadLine();
            if (ssSize.ToUpper() == "Y")
            {
                for (int i = 1; i < size.Length; i++)
                {
                    temp = size[i];
                    k = i - 1;
                    while (k >= 0 && size[k] > temp)
                    {
                        size[k + 1] = size[k];
                        k--;
                    }
                    size[k + 1] = temp;
                }
                Console.WriteLine("\nThe sorted array is as follows: ");
                for (int i = 0; i < size.Length; i++)
                {
                    Console.WriteLine(size[i]);

                }
                Console.WriteLine("Note that this uses an insertion sort.");
            }

            else
            {
                Console.WriteLine("Fine! Don't sort it --  your loss!!!");
            }
        }
        catch (System.FormatException)
        {
            Console.WriteLine("Not correct format, restarting array process.");
            Array();
        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

您正在使用

ConsoleKeyInfo key = Console.ReadKey();

它正在吃掉该行的第一个字符,因为在编写命令之前没有按ENTER键。

最佳解决方案是将ENTER替换为文字"HELP",然后坚持Console.Readline

示例:

        while (control)
        {
            Console.WriteLine("Enter EXIT to end the program, HELP for options");
            _a = Console.ReadLine();
            switch (_a.ToUpper())
            {
                case "HELP":
                    Console.WriteLine("Enter C for a constructor.");
                    Console.WriteLine("Enter M for a method.");
                    Console.WriteLine("Enter A for an array.");
                    Console.WriteLine("Enter D for a delegate.");
                    break;
                case "EXIT":
                ....
                ...

答案 1 :(得分:1)

这是因为当您在显示第一个提示后输入EXIT时,使用Console.ReadKey()来读取第一个字符。因此,_a变量中的值没有e且仅为xit

将显示选项移动到与其余选项相同的级别,问题就会消失:

    static void Main(string[] args)
    {
        // Instantiate the delegate.
        Del handler = DelegateMethod;
        string _a = "";
        constructor con = new constructor();
        bool control = true;
        while (control)
        {
            Console.WriteLine("Enter EXIT to end the program.");
            Console.WriteLine("Enter O for options");
            _a = Console.ReadLine();
            switch (_a.ToUpper())
            {
                case "EXIT":
                    Console.WriteLine("Thank you for using AJ's program.");
                    control = false;
                    break;
                case "O":
                    Console.WriteLine("Enter C for a constructor.");
                    Console.WriteLine("Enter M for a method.");
                    Console.WriteLine("Enter A for an array.");
                    Console.WriteLine("Enter D for a delegate.");
                    break;
                case "C":
                    Console.WriteLine(con.a);
                    Console.WriteLine("Would you like to test another scenario?");
                    _a = Console.ReadLine();
                    if (_a.ToUpper() == "Y")
                    {
                        continue;
                    }
                    control = false;
                    break;
                case "M":
                    metroid();
                    break;
                case "A":
                    Array();
                    break;
                case "D":
                    // call the delegate
                    handler("This is how you call a delegate. Also, Pasta noodles taste like wontons!!! =)");
                    break;
                default:
                    Console.WriteLine("No match");
                    break;
            }
        }
    }