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(); }
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(); }
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(); }