[英]How to only allow single words to be input using console.readline() in c#?
I have the following code. 我有以下代码。
Console.Clear();
string encodedMessage = "";
Console.WriteLine("Enter a word to encode");
char[] stringtoencode = Console.ReadLine().ToCharArray();
for (int i = 1; i < stringtoencode.Length; i++)
{
string currentCharAsString = stringtoencode[i].ToString();
encodedMessage += currentCharAsString;
}
encodedMessage = encodedMessage + stringtoencode[0].ToString() + "ay";
Console.WriteLine("Your string encodes to backslang as " +encodedMessage);
It takes a string input from the user and encodes it into a form of backslang (which is a phonetic encryption, which simply moves the first letter of the word to the end of the word and adds 'ay' to the end of the word) 它从用户那里获取一个字符串输入,并将其编码为后s语形式(这是一种语音加密,它只是将单词的第一个字母移到单词的末尾,并在单词的末尾添加“ ay”)
I am using Console.ReadLine() to retrieve the input string. 我正在使用Console.ReadLine()来检索输入字符串。 How can I modify the above code so that it only allows the user to enter a single word, following the prompt 'enter a word to encode'?
如何修改以上代码,使其仅允许用户在提示“输入要编码的单词”后输入一个单词?
This will ask the user to input a (new) word if the line read contains a space. 如果读取的行包含空格,这将要求用户输入(新)单词。
string word;
do
{
Console.WriteLine("Enter a word to encode");
word = Console.ReadLine();
} while (word.Contains(' '));
var encodedMessage = word.Substring(1) + word[0] + "ay";
Console.WriteLine("Your string encodes to backslang as " + encodedMessage);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.