Skip to content

Instantly share code, notes, and snippets.

@calebjross
Last active March 7, 2018 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calebjross/6a08bc2be733c1bee731 to your computer and use it in GitHub Desktop.
Save calebjross/6a08bc2be733c1bee731 to your computer and use it in GitHub Desktop.
Simple math console application - C#
using System;
namespace JamesonMath
{
class Program
{
static void Main(string[] args)
{
//variables
string userName;
int age;
string playerResponse;
string mathProblem;
string userValue;
int answer;
string keepPlaying;
bool ageQuestion;
bool answerCheck;
int num1;
int num2;
int mathAnswer;
Console.WriteLine("What is your name?");
userName = Console.ReadLine();
Console.WriteLine(userName + "! That is a wonderful name!");
//Begin player age question
ageQuestion = true;
while (ageQuestion)
{
Console.WriteLine("How old are you, " + userName + "?");
//string playerAge = Console.ReadLine();
if (int.TryParse(Console.ReadLine(), out age))
{
Console.WriteLine(age + " years old! You must be very smart.");
break;
}
else
{
Console.WriteLine("Sorry. I don't understand that answer.");
}
}
#region
//Begin asking if player wants to play a game
bool userAnswer = true;
while (userAnswer)
{
Console.WriteLine("Would you like to do some math problems? Press Y for Yes and N for No.");
playerResponse = Console.ReadLine();
if (playerResponse.ToUpper() == "YES" || playerResponse.ToUpper() == "Y")
{
Console.WriteLine("Great! Let's get started.");
Console.WriteLine("Complete the following equation");
break;
}
else if (playerResponse.ToUpper() == "NO" || playerResponse.ToUpper() == "N")
{
Console.WriteLine("Are you sure. Math is fun!");
}
else
{
Console.WriteLine("Sorry. I don't understand that answer.");
}
}
//Begin game
int correctCount = 0;
bool userPlays = true; //This is a flag that we will use to control the flow of the userPlays loop
while (userPlays)
{
//int correctCount = 0;
bool questionCycle = true;
while (questionCycle)
{
Random rnd = new Random();
num1 = rnd.Next(10);
num2 = rnd.Next(10);
mathAnswer = num1 + num2;
mathProblem = num1.ToString() + " + " + num2.ToString() + " = ";
Console.WriteLine(mathProblem);
#endregion
//Begin User Input
userValue = Console.ReadLine();
answer = Convert.ToInt32(userValue);
//Careful here--if the user doesn't enter an integer value,
//the program throws an unhandled exception. Look back at
//your use of TryParse() above. You'll want a similar approach
//here, to avoid the program breaking.
//Begin Result
if (mathAnswer == answer)
{
Console.WriteLine("Correct!");
correctCount = correctCount + 1;
//if (correctCount == 5)
if (correctCount % 5 == 0)
{
break;
}
}
else
{
Console.WriteLine("Incorrect. The Correct answer is " + mathAnswer);
}
}
//Begin "Continue playing?" question. Currently this is asked after
//every question. Ideally, this would be asked only after x number of correct answers
bool continuePlaying = true;
while (continuePlaying)
{
Console.WriteLine("You've answered " + correctCount + " questions correctly! Great job. "+
"Would you like to keep playing? Press Y for Yes and N for No.");
keepPlaying = Console.ReadLine();
{
if (keepPlaying.ToUpper() == "YES" || keepPlaying.ToUpper() == "Y")
{
Console.WriteLine();
break;
}
else if (keepPlaying.ToUpper() == "NO" || keepPlaying.ToUpper() == "N")
{
Console.WriteLine("Goodbye.");
Environment.Exit(0);
}
else
{
Console.WriteLine("Sorry. I don't understand that answer.");
}
}
}
}//end While userPlays
Console.ReadLine();
}
}
}
@calebjross
Copy link
Author

This is my very first c# console application. After watching half of the MVA C# for Absolute Beginner's series (I'm still watching them; I haven't given up) I decided to test my knowledge. This is what I came up with. I'm pretty proud of it. If anyone happens across this code and wants to give me advice (specifically on refractoring) I would love you forever.

@SlippinKareem
Copy link

Dude, did you give up yet??!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment