-3

I'm trying to write a beginner program, where I roll 2 dies, and it tests the dies, if there's any evens it will say "evens are better then odds" and if there's a scenario where there's only odds it will say, "Odds are still cool", and after each roll it will ask the user if they want to play again, if not it will tell the user how many times they rolled the die. When I run the program, it will run, and ask the user if they want to roll again, when I type "YES" it runs infinitely. I don't know if the problem has to do with how my main method is written or if it's the TinasDice method.

I've tried going about using only the if/else statement in the main method where the user's input is tested, but that immediately exits out of the program.

            TinasDice();

            Console.WriteLine("Do you want to play again?");
            string answer;
            int counter = 0;
            answer = Console.ReadLine();

            while (answer == "YES")
            {

                if (answer == "YES")
                {
                    TinasDice();
                }
                else
                {
                    Console.WriteLine("The number of times the dice was die was thrown is: " + counter);
                    Console.WriteLine("Nice game!");
                    Console.WriteLine("Thanks for playing. Come play again soon!");
                }

            }
        }
                public static void TinasDice()
                {
                Random random = new Random();

                int dice1 = new int();
                int dice2 = new int();

                dice1 = random.Next(1, 6);
                dice2 = random.Next(1, 6);

                Console.WriteLine("Hey Welcome to Tina's Dice Game.");
                Console.WriteLine("Let's start!");

                if (dice1 % 2 == 0 || dice2 % 2 == 0)
                {
                    Console.WriteLine("I got  " + dice1 + " and " + dice2);
                    Console.WriteLine("Evens are better then odds!");

                }
                else   
                {
                    Console.WriteLine("I got a " + dice1 + " and " + dice2);
                    Console.WriteLine("Odds ares still cool!");                      
                }      

I'm just trying to get the program to run incrementally after TinasDice is ran the first time, so when the user types "YES" it will run TinasDice once, and then prompt the user again, until the user types something else other then "YES".

Iseez Ice
  • 9
  • 3
  • 2
    You have a `while (answer == "YES")` loop with nothing setting it to any other value, hence an infinite loop. You need to add `answer = Console.ReadLine();` to after you call `TinasDice` – Simon Wilson Oct 05 '19 at 21:34
  • 2
    If you use a debugger it should be pretty obvious what is happening here. You are asking the question but only once and it never goes back to the question again. The debugger will show (if you weren't already aware) that the while loop is the bit that repeats so anything outside that loop is never repeated... – Chris Oct 05 '19 at 21:38

1 Answers1

0

Taking your example from above.

   TinasDice();

        Console.WriteLine("Do you want to play again?");
        string answer = "YES";
        int counter = 0;


        while (answer == "YES")
        {
            answer = Console.ReadLine();
            counter++;
            if (answer == "YES")
            {
                TinasDice();
            }
            else
            {
                Console.WriteLine("The number of times the dice was die was thrown is: " + counter);
                Console.WriteLine("Nice game!");
                Console.WriteLine("Thanks for playing. Come play again soon!");
                break;
            }

        }
    }

Note the defaulting of the "YES" and the break

Simon Wilson
  • 9,929
  • 3
  • 27
  • 24