C# Program to check for valid values

后端 未结 3 1546
陌清茗
陌清茗 2020-12-22 11:57

I need some help to remove duplicate values from the following program. I cannot use Hashset, list or anything except Arrays. I have looked at different solutions that i can

相关标签:
3条回答
  • 2020-12-22 12:20
        static void Main(string[] args)
        {
            char[] Array = new char[5];
            Console.WriteLine("Please Enter 5 Letters B/W a through j only: ");
            string letters = "abcdefghij";
    
            char[] read = Console.ReadLine().ToLower().ToCharArray();
    
            //loop through array
            for (int i = 0; i < 5; i++)
            {
                if (letters.Contains(read[i]) && !Array.Contains(read[i]))
                {
                    Array[i] = read[i];
                }
                else
                {
                    Console.WriteLine("You have entered an incorrect value");
                }
    
            }
    
            Console.WriteLine("You have Entered the following Inputs: ");
            for (int i = 0; i < Array.Length; i++)
            {
                Console.WriteLine(Array[i]);
            }
            Console.ReadKey();
        }
    

    That code should do what you want

    0 讨论(0)
  • 2020-12-22 12:28

    I think this should work for you.

    static void Main()
        {
            char[] array = new char[5];
            Console.WriteLine("Please Enter 5 Letters B/W a through j only: ");
            string letters = "abcdefghij";
    
            int counter = 0;
            while (counter < 5)
            {
                string input = Console.ReadLine().ToLower();
                char myChar = input[0];
    
                if (input.Length != 1 || array.Contains(myChar) || !letters.Contains(myChar))
                {
                    Console.WriteLine("You have entered an incorrect value");
                    continue;
                }
    
                array[counter++] = myChar;
            }
    
            Console.WriteLine("You have Entered the following Inputs: ");
            Console.WriteLine(string.Join(", ", array));
        }
    
    0 讨论(0)
  • 2020-12-22 12:37

    There are a number of ways to tackle the problem if you are only allowed to use an Array. One options is to take advantage of the internal character array of the string and perform case-insensitive lookups to ensure the entered character is valid. And again use the built-in character array of the string to only add unique characters to the final result.

    static void Main()
    {
        const string msg = "Only 5 letters between a and j are allowed.";
        const string letters = "abcdefghij";
    
        Console.WriteLine( "Please Enter 5 Letters B/W a through j only:" );
    
        // read the entire input into a string
        string input = Console.ReadLine();
    
        // verify the input is the correct length.
        if( input == null || input.Length != 5 )
        {
            Console.WriteLine( msg );
            return;
        }
    
        // store the final result w/o duplicates in a string
        string result = string.Empty;
    
        // we know there are 5 and only 5 characters in the input string,
        // so just iterate over the entire string.
        foreach( char c in input )
        {
            string value = c.ToString();
    
            // check if the character exists in the allowable characters using
            // a case insensitive lookup.
            if( letters.IndexOf( value, StringComparison.OrdinalIgnoreCase ) < 0 )
            {
                // an invalid character was found, no need to continue.
                Console.WriteLine( value + " is inavlid." + msg );
                return;
            }
    
            // add the original character to the result string if it has yet to be added.
            if( !result.Contains( value ) )
            {
                result += value;
            }
        }
    
        Console.WriteLine( "You have entered the following inputs:" );
        Console.WriteLine( result );
    }
    
    0 讨论(0)
提交回复
热议问题