0
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 - regardless of the input it stops.

What is wrong?

Matt
  • 25,467
  • 18
  • 120
  • 187
  • 1
    Please use brackets for if: `if (input == "Rock" && rps == 2) { Console.WriteLine("You Lose"); end = true; } ` – Markus Meyer Aug 12 '22 at 05:40
  • 1
    The two "end = true;" statements aren't inside the if statements, so they will always run. You need to wrap them like; "if (input == "Rock" && rps == 1) { Console.WriteLine("You Win"); end = true; }" – Joachim Isaksson Aug 12 '22 at 05:41
  • Also, please move the `Random rnd = new Random();` out of the while loop, see https://stackoverflow.com/a/3854950/2142950 – Klaus Gütter Aug 12 '22 at 05:44
  • The next time you have a logic error like this, add a debug after every line and you can see exactly where you've made your mistake. – Display name Aug 12 '22 at 05:51
  • You could just use `break;` to get out of the loop – Charlieface Aug 12 '22 at 08:24

3 Answers3

1

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;
            }
        }
    }
}
Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
1

You did it wrong. You didn't use brackets.

if (condition) 
{ 
   // more than one line
} 

if (condition) 
 //single line

Please try it like this;

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

if (input == "Rock" && rps == 2)
{
    Console.WriteLine("You Lose");
    end = true;
}
MrCodingB
  • 2,284
  • 1
  • 9
  • 22
Tanzim Siddiqee
  • 532
  • 3
  • 14
0

You need to wrap the if statement in a block

if (input == "Rock" && rps == 1)
   Console.WriteLine("You Win");
   end = true; // this line is always executed

Is not the same as

if (input == "Rock" && rps == 1)
{
   Console.WriteLine("You Win");
   end = true;
}
Kevin Bosch
  • 116
  • 4