Я не могу заставить цикл while просить пользователя ввести другое слово.
Я новичок, и мне трудно понять, где моя ошибка. Я искал в Интернете подобные решения безрезультатно. Вот что у меня есть:
Console.WriteLine("Which word is the longest?");
int howMany = 5;//Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter {0} words and I will tell you which is the longest", howMany);
string userWord = Console.ReadLine();
int counter = 0;
while (counter < howMany)
{
Console.WriteLine("Enter a word {0} > ", counter + 1);
string wordLength = (userWord.Length).ToString();
counter++;
}
Console.ReadLine();





Независимо от того, какой шаблон вы реализуете здесь, вам нужно будет снова прочитать пользовательский ввод, чего вы не делаете...
while (counter < howMany)
{
Console.WriteLine("Enter a word {0} > ", counter + 1);
string wordLength = (userWord.Length).ToString();
counter++;
userWord = Console.ReadLine(); // yehaa
}
Или, похоже, это похоже на работу для цикла for.
for (int i = 0; i < howMany; i++)
{
Console.WriteLine("Enter a word {0} > ", counter + 1);
userWord = Console.ReadLine(); // yehaa
wordLength = (userWord.Length).ToString();
...
}
Примечание: мне немного непонятна ваша логика или то, как она вам нужна. Тем не менее, я думаю, вы можете видеть, что здесь происходит
Вам нужно переместить строку чтения в цикл, Ваша программа выполняется, а затем читает строку после.
Console.WriteLine("Which word is the longest?");
int howMany = 5;//Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter {0} words and I will tell you which is the longest", howMany);
string LongestWord = "";
int counter = 0;
while (counter < howMany)
{
Console.WriteLine("Enter a word {0} > ", counter + 1);
string temp = Console.ReadLine();
if (temp.Length > LongestWord.Length) // check if the new word is longer than the current longest word
{
LongestWord = temp;
}
counter++; //update counter
}
Console.WriteLine($"{LongestWord} is the longest word"); // after exiting the loop, print the longest word
Не за что, я знаю, как сложно учиться самостоятельно!! Удачи!
Спасибо, это работает отлично. Я сравню то, что вы сделали, с тем, что сделал я, чтобы заполнить некоторые пробелы в моем обучении.