Methods in C# | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 3

Methods in C#

/*THIS IS ME LEARNING METHODS IN C# PLEASE HELP ME TO REPEAT THE METHOD TO ASK THE USER "YES OR NO" EVERY TIME. IF IT DOES NOT MATCH "yes || no" the problem where i am facing is marked between ******************************** problem ******************************** */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { alert(); void alert() { Console.WriteLine("**This might damage your PC**\nDo you want to continue: yes or no"); var choice = Console.ReadLine(); if (choice == "yes") { form(); } else if (choice == "no") { Console.WriteLine("Thank You:-)"); } else { ********************************************************************* while (choice != "yes" || choice == "no") Console.WriteLine(":-{ Choose a correct option"); Console.ReadLine(); break; ********************************************************************* } } void form() { Console.WriteLine("Hello welcome to the page!"); } // this readline is used like a getch() statement in C# for me it will hold the screen as soon as the code is finished. Console.ReadLine(); } } }

25th Aug 2019, 6:59 AM
Adarsh Mamgain
Adarsh Mamgain - avatar
3 ответов
+ 2
while (choice != "yes" && choice != "no")
25th Aug 2019, 7:16 AM
Trigger - avatar
+ 2
what thomas said, but next time you ask a question only send the important code snipped( in this case that is the code in between the asterisks)
25th Aug 2019, 7:26 AM
Cat Sauce
Cat Sauce - avatar
+ 2
1. You wrote `alert` and `form` within `main` method block. Now, I don't know C# much, but I think you better move `alert` and `form` out of `main` method block, just to be clear structurally. 2. Inside `alert` method, move the while loop up before the `if` block that checks whether <choice> equals to "yes" or "no". This way, when you exited the while loop you are left with only 2 possibilities (a "yes" or a "no"). Use `continue` to repeat the loop while <choice> neither equal to "yes" or "no". 3. IIRC in C#, string comparison is done by the help of `Equals` method. I suggest you to employ that method for checking string equality, rather than relying on the != or == operator. Talk to me if you have doubts ...
25th Aug 2019, 7:54 AM