C# Program to check for valid values

后端 未结 3 1556
陌清茗
陌清茗 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: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));
        }
    

提交回复
热议问题