C#需要不同的线路输入 [英] C# Need inputs in different lines

查看:153
本文介绍了C#需要不同的线路输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个程序,它是将作为输入,而不是只重复字母。我得到的错误,当我把一个字母的输入。
这就是我需要做的,我需要在每一行用户输入(如输入,B输入等),如果有重复的值,我需要一个错误信息,并与输入下去,如果有不正确的值,我得到另一个错误,说明这样的。我不能使用LINQ,的Hashset,也不名单,它必须是数组。

 静态无效的主要(字串[] args)
{
的char []数组=新的char [5];
Console.WriteLine(请输入5个字母/黑白只有通过记者:);
包含字母=ABCDEFGHIJ

的char [] =读取到Console.ReadLine()ToLower将()ToCharArray()。

//通过数组
环的for(int i = 0;我小于5;我++)
{
如果(letters.Contains(读作[I] )及&放大器;!Array.Contains(读取由[i]))
{
数组[I] =读[I];
}
,否则
{
Console.WriteLine(你输入了不正确的值);
}

}

Console.WriteLine(你输入下列输入:);
的for(int i = 0; I< Array.Length;我++)
{
Console.WriteLine(数组[我]);
}
Console.ReadKey();
}


解决方案

如果您希望用户输入值分别那么你就需要请求每个字符在一个循环。
是这样的:

 静态无效的主要(字串[] args)
{
常量字符串validValues​​ =ABCDEFGHIJ
变种enteredCharacters =新的char [5];
的for(int i = 0; I< enteredCharacters.Length;我++)
{
Console.WriteLine(请输入和j之间的独特个性);
VAR输入=到Console.ReadLine();
如果(input.Length == 0)
{
Console.WriteLine(你没有输入值。);
的回报;
}
如果(input.Length→1)
{
Console.WriteLine(你输入超过1个字符);
的回报;
}
变种字符=输入[0]; (!validValues​​.Contains(字符))
如果
{
Console.WriteLine(你输入无效字符);
的回报;
}
如果(enteredValues​​.Contains(字符))
{
Console.WriteLine(你已经进入了这个角色);
的回报;
}
enteredCharacters [I] =人物;
}
//进程号。
}


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, I need to get the user input in each line (like a enter, b enter, etc), if there is a duplication value i need a error message and continues with the input, and if there is incorrect value i get another error stating such. I cannot use LINQ, Hashset, nor list, it has to be arrays.

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();
}

解决方案

If you want the user to enter the values individually then you would need to request each character in a loop. Something like:

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.
}

这篇关于C#需要不同的线路输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆