如何在 C# 中使用 console.readline() 只允许输入单个单词?
How to only allow single words to be input using console.readline() in c#?
我有以下代码。
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);
它接受用户输入的字符串并将其编码为一种反斜线形式(这是一种语音加密,它只是将单词的第一个字母移动到单词的末尾并添加 'ay'到单词的末尾)
我正在使用 Console.ReadLine() 检索输入字符串。如何修改上面的代码,让它只允许用户输入一个单词,按照提示 'enter a word to encode'?
如果读取的行包含 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);
我有以下代码。
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);
它接受用户输入的字符串并将其编码为一种反斜线形式(这是一种语音加密,它只是将单词的第一个字母移动到单词的末尾并添加 'ay'到单词的末尾)
我正在使用 Console.ReadLine() 检索输入字符串。如何修改上面的代码,让它只允许用户输入一个单词,按照提示 'enter a word to encode'?
如果读取的行包含 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);