Answer the question
In order to leave comments, you need to log in
How to make an entire program repeat on demand (C#)?
It is required to ask the user if he wants to repeat the test he wrote earlier and, if the answer is yes, start the test again, otherwise finish the job.
It seems like I need to wrap all the code in do while, but I'm doing something wrong.
here is my non-working variant:
string answer;
string yes = "yes";
do
{
"Some code"
Console.WriteLine("Would you like to repeat the test? (Yes/No)");
answer = Convert.ToString(Console.ReadLine());
} while (answer == yes) ;
Answer the question
In order to leave comments, you need to log in
If the user's answer is different from yes , then the loop will end.
It is better to check the answer in a case-insensitive way, perhaps the problem is this:
string answer;
string yes = "yes";
// из yes лучше сделать константу, чтобы в этом был смысл :)
// const string yes = "yes";
// либо массив - это позволит проверять разные варианты ответов
string[] yesVariants = { "yes", "y", "true", "да", "еще бы", "только об этом и мечтаю" };
// или можно использовать регулярные выражения
do
{
// любой код здесь
Console.WriteLine("Желаете повторить тест?(Yes/No) ");
answer = Console.ReadLine();
} while (answer.Equals(yes, StringComparison.OrdinalIgnoreCase));
// проверка по массиву ответов
// } while (yesVariants.Contains(answer, StringComparer.OrdinalIgnoreCase));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question