MathGame
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace MathGame
{
public class Program
{
public static void Main()
{
int count = 0;
string st;
//this loop will run the code infinite
while (true)
{
//Generating two random num
Random randomGenerator = new Random();
int num1 = randomGenerator.Next(1, 10);
Random randomGenerator1 = new Random();
int num2 = randomGenerator1.Next(1, 10);
//Adding them together
int answer = num1 + num2;
//setting value before for checking while condition
int userAnswer = -1;
Console.WriteLine("Solve");
//if answer is wrong the repeat this loop
while (userAnswer != answer)
{
//Taking input
Console.WriteLine("{0} + {1} = ?", num1, num2);
userAnswer = int.Parse(Console.ReadLine());
//if answer is wrong
if (userAnswer != answer)
{
if (userAnswer == 0)
{
break;
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Try Again\n");
Console.ResetColor();
}
}
if (userAnswer == 0)
break;
//if answer is correct
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Correct\n");
count++;
Console.ResetColor();
}
if (count > 1)
st = "s";
else
st = "";
Console.WriteLine("\nProgram exited\nYou won {0} game{1}", count, st);
}
}
}
Comments
Post a Comment