Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Why do you have to put a 1 for roll 2

 Random numberGen = new Random();


      int roll = 0;

      int roll2 = 1;// why do you have to put 1 in there rather than a zero??

      int attempts = 0;


      Console.WriteLine("Press enter to roll the two dices");


      while (roll != roll2)

      {

        Console.ReadKey();


        roll = numberGen.Next(1, 7);

        roll2 = numberGen.Next(1, 7);


        Console.WriteLine("Roll1: " + roll);

        Console.WriteLine("Roll2: " + roll2 + "\n");


        attempts++;

      }


      Console.WriteLine("It took you " + attempts + " attempts to get the same number in both dices.");




      //for maintenance purposes

      Console.ReadLine();

Best Answer

  • HNJHNJ Member
    Accepted Answer

    Beacause if you put roll2 = 0, they would be both same (roll1 = roll2).

Answers

  • Anyone know why I have to put a 1 in there rather than a 0??

  • Thank you so much!!!

  • Yep, if they are the same number at the start all the code in the while loop will never run because they are the same numbers before the code even starts.


      int roll = 0;

          int roll2 = 1;// why do you have to put 1 in there rather than a zero??

          int attempts = 0;


          Console.WriteLine("Press enter to roll the two dices");


          while (roll != roll2)


    As you can see the two numbers are the same before the code runs, henceforth meaning that the code will never run in the first place

  • And because they are the same numbers and not equal to each other (because they are the same number) the code will never run

Sign In or Register to comment.