0

我有以下代码。

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

它接受用户输入的字符串并将其编码为一种 backslang 形式(这是一种语音加密,它只是将单词的第一个字母移动到单词的末尾并在单词的末尾添加 'ay')

我正在使用 Console.ReadLine() 来检索输入字符串。如何修改上面的代码,使其只允许用户输入一个单词,按照提示“输入要编码的单词”?

4

1 回答 1

1

如果读取的行包含空格,这将要求用户输入(新)单词。

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);
于 2015-09-29T16:25:15.780 回答