How to add a loop to the following code? using System; using System.Collections.
ID: 3879877 • Letter: H
Question
How to add a loop to the following code?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz
{
class Program
{
static void Main(string[] args)
{
//Intro text
Console.WriteLine("Created by Brandon Borin");
Console.WriteLine("Welcome to the Math QUIZ!");
Console.WriteLine("ENGR115: CheckPoint 2");
//multiple choice question
Console.WriteLine(" Question 1. Multiple Choice");
Console.WriteLine("What is 1011101+1000000 (Boolean Math)");
Console.WriteLine("A. 11011101");
Console.WriteLine("B. 10101010");
Console.WriteLine("C. 11110001");
Console.WriteLine("D. 10011101");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer1 = Console.ReadLine();
//deciding answer based on input
if (answer1.ToUpper() == "D")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//True or False question
Console.WriteLine(" Question 2. True or False:");
Console.WriteLine("When adding is 10010+1100 equal to 11110?");
Console.WriteLine(" Please input T for true, or F for False");
Console.WriteLine(" ");
string answer2 = Console.ReadLine();
if (answer2.ToUpper() == "T")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//multiple choice question
Console.WriteLine(" Question 3. Multiple Choice");
Console.WriteLine("What is 10011+1111101");
Console.WriteLine("A. 10010000");
Console.WriteLine("B. 11011100");
Console.WriteLine("C. 01100000");
Console.WriteLine("D. 10011000");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer3 = Console.ReadLine();
//deciding answer based on input
if (answer3.ToUpper() == "A")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//True or False question
Console.WriteLine(" Question 4. True or False:");
Console.WriteLine("Is 1001100+1100101 equial to 10110001");
Console.WriteLine(" Please input T for true, or F for False");
Console.WriteLine(" ");
string answer4 = Console.ReadLine();
if (answer4.ToUpper() == "T")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//Multiple choice question
Console.WriteLine(" Question 5. Multiple Choice");
Console.WriteLine("Add 10011001 + 100111");
Console.WriteLine("A. 11100000");
Console.WriteLine("B. 11000000");
Console.WriteLine("C. 01100000");
Console.WriteLine("D. 11001000");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer5 = Console.ReadLine();
//deciding answer based on input
if (answer5.ToUpper() == "B")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//multiple choice question
Console.WriteLine(" Question 6. Multiple Choice");
Console.WriteLine("Add 11000011 + 101111");
Console.WriteLine("A. 10110101");
Console.WriteLine("B. 01111011");
Console.WriteLine("C. 11010010");
Console.WriteLine("D. 11110010");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer6 = Console.ReadLine();
//deciding answer based on input
if (answer6.ToUpper() == "D")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//True or False question
Console.WriteLine(" Question 7. True or False:");
Console.WriteLine("When adding in boolean is 0110+0111 equal to 1101?");
Console.WriteLine(" Please input T for true, or F for False");
Console.WriteLine(" ");
string answer7 = Console.ReadLine();
if (answer7.ToUpper() == "T")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//multiple choice question
Console.WriteLine(" Question 8. Multiple Choice");
Console.WriteLine("What is 0011+0111");
Console.WriteLine("A. 1010");
Console.WriteLine("B. 1111");
Console.WriteLine("C. 0101");
Console.WriteLine("D. 1100");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer8 = Console.ReadLine();
//deciding answer based on input
if (answer8.ToUpper() == "A")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//True or False question
Console.WriteLine(" Question 9. True or False:");
Console.WriteLine("Is 1111+1010 equal to 101010");
Console.WriteLine(" Please input T for true, or F for False");
Console.WriteLine(" ");
string answer9 = Console.ReadLine();
if (answer9.ToUpper() == "F")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//Multiple choice question
Console.WriteLine(" Question 10. Multiple Choice");
Console.WriteLine("Add 1010 + 1010");
Console.WriteLine("A. 11000");
Console.WriteLine("B. 10101");
Console.WriteLine("C. 10100");
Console.WriteLine("D. 11100");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer10 = Console.ReadLine();
//deciding answer based on input
if (answer10.ToUpper() == "C")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
}
}
}
}
Explanation / Answer
Hi,
Generally, we use loops if a set of statements repeat again and again. In order to reduce the code length and eliminate the redundancy, we use loops. Since your statements does not contain any repeated code,there is no need to use loops to avoid code redundancy. But still we can use loops to give user a choice to play the same quiz again and again as many times as user needs. Below is the modified code-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz
{
class Program
{
static void Main(string[] args)
{
string choice;
//Intro text
do
{
Console.WriteLine("Created by Brandon Borin");
Console.WriteLine("Welcome to the Math QUIZ!");
Console.WriteLine("ENGR115: CheckPoint 2");
//multiple choice question
Console.WriteLine(" Question 1. Multiple Choice");
Console.WriteLine("What is 1011101+1000000 (Boolean Math)");
Console.WriteLine("A. 11011101");
Console.WriteLine("B. 10101010");
Console.WriteLine("C. 11110001");
Console.WriteLine("D. 10011101");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer1 = Console.ReadLine();
//deciding answer based on input
if (answer1.ToUpper() == "D")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//True or False question
Console.WriteLine(" Question 2. True or False:");
Console.WriteLine("When adding is 10010+1100 equal to 11110?");
Console.WriteLine(" Please input T for true, or F for False");
Console.WriteLine(" ");
string answer2 = Console.ReadLine();
if (answer2.ToUpper() == "T")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//multiple choice question
Console.WriteLine(" Question 3. Multiple Choice");
Console.WriteLine("What is 10011+1111101");
Console.WriteLine("A. 10010000");
Console.WriteLine("B. 11011100");
Console.WriteLine("C. 01100000");
Console.WriteLine("D. 10011000");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer3 = Console.ReadLine();
//deciding answer based on input
if (answer3.ToUpper() == "A")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//True or False question
Console.WriteLine(" Question 4. True or False:");
Console.WriteLine("Is 1001100+1100101 equial to 10110001");
Console.WriteLine(" Please input T for true, or F for False");
Console.WriteLine(" ");
string answer4 = Console.ReadLine();
if (answer4.ToUpper() == "T")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//Multiple choice question
Console.WriteLine(" Question 5. Multiple Choice");
Console.WriteLine("Add 10011001 + 100111");
Console.WriteLine("A. 11100000");
Console.WriteLine("B. 11000000");
Console.WriteLine("C. 01100000");
Console.WriteLine("D. 11001000");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer5 = Console.ReadLine();
//deciding answer based on input
if (answer5.ToUpper() == "B")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//multiple choice question
Console.WriteLine(" Question 6. Multiple Choice");
Console.WriteLine("Add 11000011 + 101111");
Console.WriteLine("A. 10110101");
Console.WriteLine("B. 01111011");
Console.WriteLine("C. 11010010");
Console.WriteLine("D. 11110010");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer6 = Console.ReadLine();
//deciding answer based on input
if (answer6.ToUpper() == "D")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//True or False question
Console.WriteLine(" Question 7. True or False:");
Console.WriteLine("When adding in boolean is 0110+0111 equal to 1101?");
Console.WriteLine(" Please input T for true, or F for False");
Console.WriteLine(" ");
string answer7 = Console.ReadLine();
if (answer7.ToUpper() == "T")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//multiple choice question
Console.WriteLine(" Question 8. Multiple Choice");
Console.WriteLine("What is 0011+0111");
Console.WriteLine("A. 1010");
Console.WriteLine("B. 1111");
Console.WriteLine("C. 0101");
Console.WriteLine("D. 1100");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer8 = Console.ReadLine();
//deciding answer based on input
if (answer8.ToUpper() == "A")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//True or False question
Console.WriteLine(" Question 9. True or False:");
Console.WriteLine("Is 1111+1010 equal to 101010");
Console.WriteLine(" Please input T for true, or F for False");
Console.WriteLine(" ");
string answer9 = Console.ReadLine();
if (answer9.ToUpper() == "F")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
Console.WriteLine(" Next Question...");
}
//Multiple choice question
Console.WriteLine(" Question 10. Multiple Choice");
Console.WriteLine("Add 1010 + 1010");
Console.WriteLine("A. 11000");
Console.WriteLine("B. 10101");
Console.WriteLine("C. 10100");
Console.WriteLine("D. 11100");
Console.WriteLine(" ");
Console.WriteLine("Please input A, B, C, or D.");
//this is where you read what the user inputs and assign it to a new string
string answer10 = Console.ReadLine();
//deciding answer based on input
if (answer10.ToUpper() == "C")
{
Console.WriteLine("***Correct!***");
Console.WriteLine(" Next Question...");
}
else
{
Console.WriteLine("***Wrong!***");
}
Console.WriteLine("Want to play the quiz again(Y or N) ?");
choice=Console.ReadLine();
}while(choice=="Y");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.