I
In3rstella
Guest
I'm new to C# and decided to make a console project that would quiz me on some C# operators. Originally I wanted to use a loop that used the Random class to generate a number within the range of my array containing questions.
After the random index number was generated and the question outputted from the array to the user it would then remove the question (so it wouldn't repeat the same question) from the array's index and shorten the range that would need to be generated. I spent some time searching online to get an idea on how I would go about removing an element from the array and then looping through the process again but wasn't able to come up with anything that made sense.
So I decided to try a lengthy approach that I knew would give me somewhat of the solution I originally intended. Here is my code below:
public static void OperatorQs()
{
//Created an instance of Random for the purpose of generating a number range that would meet the index of the firstOperators array
Random rand = new Random();
//Array containing questions on operators
string[] firstOperators = { "1) Adds two operands", "2) Subtracts second operand from the first", "3) Multiplies both operands", "4) Divides numerator by de-numerator", "5) Modulus Operator and remainder of after an integer division",
"6) Increment operator increases integer value by one","7) Decrement operator decreases integer value by one"};
//Loop that I want to run until all questions from array have been displayed.
foreach (string st in firstOperators)
{
Console.WriteLine(firstOperators[0]);
string questAnswer1 = Console.ReadLine();
if (questAnswer1 == "+")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Console.WriteLine(firstOperators[1]);
string questAnswer2 = Console.ReadLine();
if (questAnswer2 == "-")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Console.WriteLine(firstOperators[2]);
string questAnswer3 = Console.ReadLine();
if (questAnswer3 == "*")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Console.WriteLine(firstOperators[3]);
string questAnswer4 = Console.ReadLine();
if (questAnswer4 == "/")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Console.WriteLine(firstOperators[4]);
string questAnswer5 = Console.ReadLine();
if (questAnswer5 == "%")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Console.WriteLine(firstOperators[5]);
string questAnswer6 = Console.ReadLine();
if (questAnswer6 == "++")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Lengthy I know. I wasn't exactly sure how to go about looping a single question again if it was answered incorrectly but I knew I could run through the loop again using a continue statement in a else block. (Again i'm new and any help is much appreciated and helps me improve!)
Continue reading...
After the random index number was generated and the question outputted from the array to the user it would then remove the question (so it wouldn't repeat the same question) from the array's index and shorten the range that would need to be generated. I spent some time searching online to get an idea on how I would go about removing an element from the array and then looping through the process again but wasn't able to come up with anything that made sense.
So I decided to try a lengthy approach that I knew would give me somewhat of the solution I originally intended. Here is my code below:
public static void OperatorQs()
{
//Created an instance of Random for the purpose of generating a number range that would meet the index of the firstOperators array
Random rand = new Random();
//Array containing questions on operators
string[] firstOperators = { "1) Adds two operands", "2) Subtracts second operand from the first", "3) Multiplies both operands", "4) Divides numerator by de-numerator", "5) Modulus Operator and remainder of after an integer division",
"6) Increment operator increases integer value by one","7) Decrement operator decreases integer value by one"};
//Loop that I want to run until all questions from array have been displayed.
foreach (string st in firstOperators)
{
Console.WriteLine(firstOperators[0]);
string questAnswer1 = Console.ReadLine();
if (questAnswer1 == "+")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Console.WriteLine(firstOperators[1]);
string questAnswer2 = Console.ReadLine();
if (questAnswer2 == "-")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Console.WriteLine(firstOperators[2]);
string questAnswer3 = Console.ReadLine();
if (questAnswer3 == "*")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Console.WriteLine(firstOperators[3]);
string questAnswer4 = Console.ReadLine();
if (questAnswer4 == "/")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Console.WriteLine(firstOperators[4]);
string questAnswer5 = Console.ReadLine();
if (questAnswer5 == "%")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Console.WriteLine(firstOperators[5]);
string questAnswer6 = Console.ReadLine();
if (questAnswer6 == "++")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong");
continue;
}
Lengthy I know. I wasn't exactly sure how to go about looping a single question again if it was answered incorrectly but I knew I could run through the loop again using a continue statement in a else block. (Again i'm new and any help is much appreciated and helps me improve!)
Continue reading...