public static void GetNumberGuesser()
        {
            int numOfPlayers;                    // Local variable.

            Random     rnd   = new Random();     // Create a new random instance.
            List <int> score = new List <int>(); // Create a list of integers.

            Console.WriteLine("\nWelcome to Number Guesser!");
            Console.WriteLine("This program allows you try to guess a random number between 9 and 100.");
            Console.WriteLine("You can play by yourself or with friends.\n");
            Console.Write("How many players wish to play > ");
            numOfPlayers = TryParse.IntTryParse(Console.ReadLine());

            // For loop to iterate through for each player.
            for (int i = 1; i <= numOfPlayers; i++)
            {
                int  userGuess;
                int  count     = 1;
                bool correct   = false;
                int  number    = rnd.Next(9, 101);
                int  rangeNum  = rnd.Next(1, 25);
                int  rangeNum2 = rnd.Next(1, 25);

                // Run do while loop until user guesses right answer.
                do
                {
                    Console.Write($"\nPlayer {i} enter your guess");
                    Console.Write(" > ");
                    userGuess = TryParse.IntTryParse(Console.ReadLine());

                    // Run through this loop if user guesses wrong answer.
                    if (userGuess != number)
                    {
                        Console.WriteLine("Incorrect guess!");
                        Console.WriteLine($"The number is between {number - rangeNum} and {number + rangeNum2}.");
                        Console.WriteLine();
                        count++;    // Accumulation for incorrect guesses.
                    }
                    else if (userGuess == number)
                    {
                        correct = true;
                    }
                } while (correct == false);

                score.Add(count);   // Add the amount of guesses from user to list.
            }

            Console.WriteLine();
            for (int j = 0; j < score.Count; j++)       // Displays the contents of the list.
            {
                Console.WriteLine($"Player {j+1} Total Guesses: {score[j]}");
            }
            Console.WriteLine();
        }
Esempio n. 2
0
        public static void GetParentEntry()
        {
            // Local variables.
            bool   exit          = false;
            double firstNumber   = 0;
            string symbol        = null;
            double secondNumber  = 0;
            double correctAnswer = 0;
            double answer        = 0;

            try
            {
                Console.WriteLine("Parent/Teacher please enter your 10 problems and solutions.");
                Console.WriteLine("Enter the first number, operator, second number, and the solution.");

                // Create a CSV file.
                StreamWriter outputFile = File.CreateText("Parent_Teacher.csv");
                // Write the initial values to the first line of the CSV file.
                outputFile.WriteLine($"{firstNumber},{symbol},{secondNumber},{answer}");

                // Run through loop 10 times.
                for (int entries = 1; entries <= 10; entries++)
                {
                    Console.Write($"\nProblem {entries}: Enter your First Number > ");
                    firstNumber = TryParse.DoubleTryParse(Console.ReadLine());

                    Console.Write($"\nProblem {entries}: Enter your Symbol > ");
                    symbol = TryParse.SymbolParse(Console.ReadLine());

                    Console.Write($"\nProblem {entries}: Enter your Second Number > ");
                    secondNumber = TryParse.DoubleTryParse(Console.ReadLine());

                    // Send user input to method to get correct answer.
                    correctAnswer = ValidateAnswer(firstNumber, symbol, secondNumber);

                    do
                    {
                        // Get user answer and compare it to correct answer.
                        Console.Write($"\nProblem {entries}: Enter your Answer > ");
                        answer = TryParse.DoubleTryParse(Console.ReadLine());

                        if (answer != correctAnswer)
                        {
                            Console.Write("Your answer doesn't match the correct answer!");
                        }
                        else
                        {
                            exit = true;
                        }
                    } while (exit == false);

                    // Write content to CSV file.
                    outputFile.WriteLine($"{firstNumber},{symbol},{secondNumber},{answer}");
                }
                // Close the CSV file.
                outputFile.Close();
            }
            catch
            {
                Console.WriteLine("Cannot read file!");
            }
        }
Esempio n. 3
0
        public static void GetChildAnswers()
        {
            // Local variables
            int           correctUserAnswers = 0;
            List <double> firstNumber        = new List <double>();
            List <string> symbol             = new List <string>();
            List <double> secondNumber       = new List <double>();
            List <double> answers            = new List <double>();


            // Open the file.
            using (StreamReader fileReader = new StreamReader("Parent_Teacher.csv"))
            {
                // Reads each line of the file.
                fileReader.ReadLine();

                // Reads all lines of file until end
                while (!fileReader.EndOfStream)
                {
                    // Split line on comma.
                    var line   = fileReader.ReadLine();
                    var values = line.Split(',');

                    // Add values to lists.
                    firstNumber.Add(double.Parse(values[0]));
                    symbol.Add(values[1].ToString());
                    secondNumber.Add(double.Parse(values[2]));
                    answers.Add(double.Parse(values[3]));
                }
            }

            Console.WriteLine("\nWelcome to DataBank!");
            Console.WriteLine("This program will give you 10 math problems that you can solve.");
            Console.WriteLine("You will get two attempts to get the answer correct.");

            for (int i = 0; i < answers.Count(); i++)
            {
                int    attempts   = 0;
                double userAnswer = -1000000;

                // While user answer is incorrect and haven't answered twice.
                while (attempts < 2 && userAnswer != answers[i])
                {
                    // Get user answer.
                    Console.Write($"\n{firstNumber[i]} {symbol[i]} {secondNumber[i]} = ");
                    userAnswer = TryParse.DoubleTryParse(Console.ReadLine());

                    if (userAnswer == answers[i])       // Compare user answer to correct answer.
                    {
                        Console.WriteLine($"Correct! {firstNumber[i]} {symbol[i]} {secondNumber[i]} = {userAnswer}");
                        correctUserAnswers++;
                    }
                    else
                    {
                        Console.WriteLine("Incorrect.");
                    }
                    attempts++;     // Accumulate attempts.
                }
            }
            Console.WriteLine();
            Console.WriteLine($"Correct Answers: {correctUserAnswers}");
            Console.WriteLine($"Total Questions: {answers.Count}");
            Console.WriteLine();
        }
        public static void GetAnswerChecker()
        {
            // Named constant
            const int QUESTION_AMOUNT = 10;
            // Local variables
            int num1;
            int num2;
            int answeredRight = 0;

            Console.WriteLine("\nWelcome to Answer Checker!");
            Console.WriteLine("This program allows you enter a math problem");
            Console.WriteLine("and the program will give you the correct answer.");

            for (int index = 0; index < QUESTION_AMOUNT; index++)
            {
                // Inline variables to allow for reset in the forloop
                string symbol        = " ";
                int    userAnswer    = 0;
                int    correctAnswer = 1000000;
                int    attempts      = 0;

                // Gets the user's first number for calculation by entering class method for validation.
                Console.Write("\nFirst Number: > ");
                num1 = TryParse.IntTryParse(Console.ReadLine());

                // Gets the user's symbol for calculation by entering class method for validation.
                Console.Write("Enter a symbol (+, -, /, *): > ");
                symbol = TryParse.SymbolParse(Console.ReadLine());

                // Gets the user's second number for calculation by entering class method for validation.
Second:
                Console.Write("Second Number: > ");
                num2 = TryParse.IntTryParse(Console.ReadLine());

                // while loop to make sure the user gets 2 attempts unless they get the right answer
                while (attempts < 2 && userAnswer != correctAnswer)
                {
                    // Gets the user's answer for calculation by entering class method for validation.
                    Console.Write("Answer: > ");
                    userAnswer = TryParse.IntTryParse(Console.ReadLine());

                    // if statement allows for creation of the correct answer based on symbol user entered
                    if (symbol.Contains("+"))
                    {
                        correctAnswer = num1 + num2;
                    }
                    else if (symbol.Contains("-"))
                    {
                        correctAnswer = num1 - num2;
                    }
                    else if (symbol.Contains("/"))
                    {
                        // Stops the user from using zero. goto to send user to the second answer input.
                        if (num2 != 0)
                        {
                            correctAnswer = num1 / num2;
                        }
                        else
                        {
                            Console.WriteLine("Can not divide by zero");
                            goto Second;
                        }
                    }
                    else if (symbol.Contains("*"))
                    {
                        correctAnswer = num1 * num2;
                    }
                    else
                    {
                        Console.WriteLine("Invalid input");
                    }

                    // Displays if correct
                    if (userAnswer == correctAnswer)
                    {
                        Console.WriteLine($"Correct! {num1} {symbol} {num2} = {userAnswer}");
                        answeredRight++;
                    }
                    else
                    {
                        Messages.ErrorMessage();
                    }

                    attempts++;
                }

                // Displays after second incorrect answer
                if (userAnswer != correctAnswer)
                {
                    Console.WriteLine($"{num1} {symbol} {num2} = {correctAnswer}");
                }
            }
            Console.WriteLine();
            Console.WriteLine($"Correct Answers: {answeredRight}");     // displays amount answered right
            Console.WriteLine($"Total Questions: {QUESTION_AMOUNT}");   // dipslays total amount of questions asked
            Console.WriteLine();
        }
Esempio n. 5
0
        public static void GetRandomProblem()
        {
            // Named constants
            const int QUESTION_AMOUNT = 10;

            // Local variables
            Random rand = new Random();
            int    num1 = 0;
            int    num2 = 0;

            char[] operators     = { '+', '-', '*', '/' };
            char   randomOp      = ' ';
            int    userAnswer    = -1;
            int    correctAnswer = 0;
            int    correct       = 0;

            // Display description of the Memory Bank activity
            Console.WriteLine("\nWelcome to Memory Bank!");
            Console.WriteLine("This program will give you 10 math problems that you can solve.");
            Console.WriteLine("You will get two attempts to get the answer correct.");
            Console.WriteLine("NOTE: Round answers down to the previous integer.");

            // Consume the next line for appearance
            Console.WriteLine("");

            // Loop for creating math problems
            for (int i = 0; i < QUESTION_AMOUNT; i++)
            {
                int attempts = 0;
                num1     = rand.Next(1, 10);
                num2     = rand.Next(1, 10);
                randomOp = operators[rand.Next(operators.Length)];

                switch (randomOp)
                {
                case '+':
                    correctAnswer = num1 + num2;
                    break;

                case '-':
                    correctAnswer = num1 - num2;
                    break;

                case '*':
                    correctAnswer = num1 * num2;
                    break;

                case '/':
                    correctAnswer = num1 / num2;
                    break;
                }

                while (attempts < 2 && userAnswer != correctAnswer)
                {
                    Console.Write($"{num1} {randomOp} {num2} = ");
                    userAnswer = TryParse.IntTryParse(Console.ReadLine());

                    if (userAnswer == correctAnswer)
                    {
                        Console.WriteLine("Correct!");
                        correct++;
                    }
                    else
                    {
                        Console.WriteLine("Incorrect!");
                    }
                    attempts++;
                }

                // Consume next line for appearance
                Console.WriteLine("");
            }

            Console.WriteLine();
            Console.WriteLine($"Correct Answers: {correct}");         // displays amount answered right
            Console.WriteLine($"Total Questions: {QUESTION_AMOUNT}"); // dipslays total amount of questions asked
            Console.WriteLine();
        }