C# Need inputs in different lines

后端 未结 3 1721
伪装坚强ぢ
伪装坚强ぢ 2020-12-22 11:51

I am trying to create a program that is will take letters as input only and not duplicated. I am getting error when i put one letter in an input. This is what i need to do,

3条回答
  •  情歌与酒
    2020-12-22 12:30

    I think this more or less does what you want:

    var max = 5;
    var array = new char[max];
    var letters = "abcdefghij";
    
    var count = 0;
    while (count < 5)
    {
        Console.WriteLine("Please Enter {0} Letters B/W a through j only: ", max);
    
        var key = Console.ReadKey();
        var read = key.KeyChar
    
        if (!letters.Contains(read))
        {
            Console.WriteLine("You have entered an incorrect value");
            continue;
        }
    
        var found = false;
        for (int i = 0; i < count; i++)
        {
            if (array[i] == read)
            {
                found = true;
            }
        }
    
        if (found)
        {
            Console.WriteLine("You have entered an duplicate value");
            continue;
        }
    
        array[count++] = read;
    }
    
    Console.WriteLine("You have Entered the following Inputs: ");
    for (int i = 0; i < array.Length; i++)
    {
        Console.WriteLine(array[i]);
    }
    
    Console.ReadKey();
    

提交回复
热议问题