In: Computer Science
[C#] Randomly generate two numbers that user chooses and then ask the user what the answer is when some operator is applied. If user is correct, they will be congratulated. If they are wrong, they will be given the correct answer. This should be repeated based on how many times is chosen by user. I have the code which will do the following below:
User wants to know the answer to x % y,
What is smallest value of x: (user input)
What is largest value of x: (user input)
What is smallest value of y: (user input)
What is smallest value of y: (user input)
How many times should it happen?: (user input)
What I need help with is now randomly generating the x and y the user chooses and then using the operator that they decide whether it be %,/,+, or - and ask them what the answer is, and determine whether user is correct or not. For example if range of x is between 10 and 50 and y is 30 and 80 and the user chooses to be tested on %, the program will ask the user the amount of time that they specified. For this example let's say it is 3. They are then given 3 problems featuring numbers from the range above and they are then tested on it.
using System.IO;
using System;
class Program
{
static void Main()
{
int x1, x2, y1, y2;
Console.WriteLine("What is smallest value of x:");
x1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is largest value of x:");
x2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is smallest value of y:");
y1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is largest value of y:");
y2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the operator:");
string op = Console.ReadLine();
Console.WriteLine("How many times should it happen?:");
int n = Convert.ToInt32(Console.ReadLine());
Random random = new Random();
for(int i=1;i<=n;i++) {
int x = random.Next(x1, x2);
int y = random.Next(y1, y2);
Console.Write("What is the result of "+x+" "+op+" "+y+" = ");
int answer = Convert.ToInt32(Console.ReadLine());
int result = 0;
if(op == "+") {
result = x + y;
} else if(op == "-") {
result = x - y;
} else if(op == "*") {
result = x * y;
} else if(op == "%") {
result = x % y;
} else {
result = x / y;
}
if(result == answer) {
Console.WriteLine("Congratulations");
} else {
Console.WriteLine("Wrong answer. Correct answer is "+result);
}
}
}
}
Output:
$mcs *.cs -out:main.exe $mono main.exe What is smallest value of x: What is largest value of x: What is smallest value of y: What is largest value of y: Enter the operator: How many times should it happen?: What is the result of 55 + 31 = 86 Congratulations What is the result of 50 + 30 = 70 Wrong answer. Cor
rect answer is 80