Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While loop not looping – C#

internal class Program
{
    private static void Main(string[] args)
    {
        bool end = false;

        while (end != true)
        {
            Random rnd = new Random();
            int rps = rnd.Next(1, 4);

            Console.WriteLine("Rock Paper Scissors");
            string input = Console.ReadLine().Trim(); 

            if (input == "Rock" && rps == 1)
                Console.WriteLine("You Win");
            end = true;

            if (input == "Rock" && rps == 2)
                Console.WriteLine("You Lose");
            end = true;

            if (input == "Rock" && rps == 3)
                Console.WriteLine("Tie, play again");
        }
    }
}

In this program, I am trying to create a rock paper scissors program however, when it rps == 3 and its a tie, instead of starting from the beginning of the while statement, it does not do anything.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As mentioned in comment, you didn’t use braces {} properly. I also made it a bit better, so you can use this :

internal class Program
{
    private static void Main(string[] args)
    {
        bool end = false;
        Random rnd = new Random();
        int rps;
        string input;
        
        while (end != true)
        {
            end = true;
            rps = rnd.Next(1, 4);

            Console.WriteLine("Rock Paper Scissors");
            input = Console.ReadLine().Trim(); 

            if (input == "Rock" && rps == 1)
                Console.WriteLine("You Win");

            if (input == "Rock" && rps == 2)
                Console.WriteLine("You Lose");

            if (input == "Rock" && rps == 3)
            {
                Console.WriteLine("Tie, play again");
                end = false;
            }
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading