Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#需要不同行的输入_C# - Fatal编程技术网

C#需要不同行的输入

C#需要不同行的输入,c#,C#,我正在尝试创建一个程序,它将只接受字母作为输入,而不重复。我在输入中输入一个字母时出错。 这就是我需要做的,我需要在每一行中获得用户输入(如a enter、b enter等),如果有重复值,我需要一条错误消息并继续输入,如果有不正确的值,我会得到另一个错误说明。我不能使用LINQ、Hashset或list,它必须是数组 static void Main(string[] args) { char[] Array = new char[5]; Console.WriteLine("

我正在尝试创建一个程序,它将只接受字母作为输入,而不重复。我在输入中输入一个字母时出错。 这就是我需要做的,我需要在每一行中获得用户输入(如a enter、b enter等),如果有重复值,我需要一条错误消息并继续输入,如果有不正确的值,我会得到另一个错误说明。我不能使用LINQ、Hashset或list,它必须是数组

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();
}
static void Main(字符串[]args)
{
char[]数组=新字符[5];
Console.WriteLine(“请仅输入5个字母B/W a至j:”;
字符串字母=“abcdefghij”;
char[]read=Console.ReadLine().ToLower().ToCharArray();
//循环阵列
对于(int i=0;i<5;i++)
{
if(letters.Contains(read[i])&&!Array.Contains(read[i]))
{
数组[i]=读取[i];
}
其他的
{
Console.WriteLine(“您输入的值不正确”);
}
}
WriteLine(“您已输入以下输入:”);
for(int i=0;i
这是你的问题

用于(int i=0;i<5;i++)

这是您的解决方案:

 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 < read.Length; 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();
            }
static void Main(字符串[]args)
{
char[]数组=新字符[5];
Console.WriteLine(“请仅输入5个字母B/W a至j:”;
字符串字母=“abcdefghij”;
char[]read=Console.ReadLine().ToLower().ToCharArray();
//循环阵列
for(int i=0;i
我认为这或多或少符合您的要求:

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();
var max=5;
var数组=新字符[max];
var letters=“abcdefghij”;
var计数=0;
同时(计数<5)
{
WriteLine(“请输入{0}个字母B/W a到j:”,max);
var key=Console.ReadKey();
var read=key.KeyChar
如果(!letters.Contains(read))
{
Console.WriteLine(“您输入的值不正确”);
继续;
}
var=false;
for(int i=0;i
如果希望用户单独输入值,则需要请求循环中的每个字符。 比如:

static void Main(string[] args)
{
    const string validValues = "abcdefghij";
    var enteredCharacters = new char[5];
    for (int i = 0; i < enteredCharacters.Length; i++)
    {
        Console.WriteLine("Please enter a unique character between a and j");
        var input = Console.ReadLine();
        if (input.Length == 0)
        {
            Console.WriteLine("You did not enter a value.");
            return;
        }
        if (input.Length > 1)
        {
            Console.WriteLine("You have entered more than 1 character");
            return;
        }
        var character = input[0];
        if (!validValues.Contains(character))
        {
            Console.WriteLine("You have entered an invalid character");
            return;
        }
        if (enteredValues.Contains(character))
        {
            Console.WriteLine("You have already entered this character");
            return;
        }
        enteredCharacters[i] = character;
    }
    // process numbers.
}
static void Main(字符串[]args)
{
常量字符串有效值=“abcdefghij”;
var enteredCharacters=新字符[5];
for(int i=0;i1)
{
Console.WriteLine(“您输入了超过1个字符”);
返回;
}
变量字符=输入[0];
如果(!validValues.Contains(字符))
{
Console.WriteLine(“您输入了无效字符”);
返回;
}
if(输入值.包含(字符))
{
Console.WriteLine(“您已经输入了此字符”);
返回;
}
输入字符[i]=字符;
}
//进程编号。
}

你的想法是把这5个字母排成一行?一种可能的解决方案是将Console.ReadLine包装在do while循环中,对于来自用户的每个输入,您可以通过他的输入进行循环,以检查有效输入。如果对每个字符循环5个字母作为in-loop,并将它们添加到一个仅允许唯一值的hashset,那么在每次迭代结束时,hashset应该有5个元素,如果没有,则重新向用户发出请求,以输入一组有效的字母。验证后,您可以跳出循环并处理用户输入。实际上Contains()是Linq扩展。可能重复您在2天内问了4次相同的问题;请在提问时查看,