I'm currently teaching myself C# and came up with a program idea as follows:
- Program generates random number
- Program generates random math operator
- Program generates random number
- Asks user for the answer to the randomly generated problem
- User inputs answer
- Program compares result of generated problem against user's input
- Program prints whether user was correct or incorrect.
The issue I'm running into is that I can only print the equation in the following format/data types:
(example) 5 + 1 (number, string operator, number)
OR combine the above data types into a single string
From there the problem is that I cannot figure out how to convert all of it into a math equation that can be stored in a variable and then compared against the user's input.
I'm sure I'm making it more difficult than it probably is, but I've been at this program, forums, and tons of posts without being able to piece anything together.
Any insight to my code and where I'm going wrong would be greatly appreciated in my learning C#!
public static void Main(string[] args)
{
mathProblem();
int userAnswer = Convert.ToInt32(Console.ReadLine());
}
public static void mathProblem()
{
Random numberGenerator = new Random();
Random operatorSelect = new Random();
//Generates random number for 1st part of problem
int number1 = numberGenerator.Next(1, 11);
//Generates random number for 2nd part of problem
int number2 = numberGenerator.Next(1, 11);
//int to store/assign random operator
int mathOperator = 0;
//newOperator set to empty string that will change and store operator generated
string newOperator = "";
// gives value of 1-4
mathOperator = operatorSelect.Next(5);
switch (mathOperator)
{
case 1:
newOperator = "+";
break;
case 2:
newOperator = "-";
break;
case 3:
newOperator = "*";
break;
case 4:
newOperator = "/";
break;
default:
newOperator = "+";
break;
}
Convert.ToString(number1);
Convert.ToString(number2);
Console.WriteLine("What is " + number1 + " " + newOperator + " " + number2 + "?");
}