I'm currently attending ERAU (online), and in my Computing for Engineers class we are learning C#. An ongoing project throughout the class is to build a "quiz" program that asks 10 questions, in order, and then repeats missed questions (in order) on a second iteration. There are 2 types: T/F and multiple choice. 4 T/F and 6 mc; the mc questions can have 4 possible answers. The program must keep track of the number of each type of question that was answered correctly and display that at the end.
I'm just learning about loops, conditional statements, arrays and methods (last week), and here's the code I came up with (it compiles and runs, and does exactly what I want it to do, but my instructor said it is too long and not optimized):
using System;
using System.Threading;
namespace Quiz
{
class Program
{
static void Main(string[] args)
{
//start the quiz by telling the student the details of the structure of the quiz and the subject being tested
Console.WriteLine("Kenneth Myers, ENGR115, Assignment: Checkpoint 4 & 5");
Console.WriteLine("You are about to take a 10 question quiz on Binary Number Systems.");
Console.WriteLine("Each question will be displayed with either a True/False or Multiple-choice answer key. Select the correct answer by pressing the corresponding key.");
Console.WriteLine("If you get the question wrong, you will be notified of the wrong answer and the next question will be displayed.");
Console.WriteLine("Once all questions have been displayed once, questions answered incorrectly will be displayed in the original order; you may attempt the question again.");
Console.WriteLine("After all missed questions have been displayed a second time and answered, the results of your quiz will be displayed, by category of question and number answered correctly, and the total number answered correctly.");
Thread.Sleep(5000);//adds a 5 second delay before the next operation to give the user time to read the instructions
Console.Clear(); //clears the screen before starting the test
//create an array with all the questions and possible answers to display to the user
var questions = new string[] { "Question 1: True or False – Binary numbers consist of a sequence of ones and zeros? Press T or F and enter to submit", "Question 2: Multiple choice – Add the following binary numbers: 1011+0011. Answers: 1) 1110, 2) 1022, 3) 1010, 4) 1032. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 3: Multiple choice – Add the following binary numbers: 11011011+01100110. Answers: 1) 11000001, 2) 11111111, 3) 12111121, 4) 101000001. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 4: True or False – Binary numbers are base-10? Press T or F and enter to submit", "Question 5: Multiple choice – Subtract the following binary numbers: 1110-0101. Answers: 1) 1010, 2) 1001, 3) 1000, 4) 0100. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 6: Multiple choice – Subtract the following binary numbers: 10010011-01101100. Answers: 1) 01101111, 2) 00010111, 3) 00100111, 4) 11011101. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 7: True or False – Binary numbers are base-2? Press T or F and enter to submit", "Question 8: Multiple choice – the binary number 1011 equates to what base-10 number? Answers: 1) 11, 2) 22, 3) 14, 4) 7. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 9: Multiple choice – what is the binary equivalent of the base-10 number 127? Answers: 1) 01101111, 2) 11111111, 3) 10111011, 4) 11111110. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 10: True or False: an 8-bit binary number can have a maximum value of 128 in base-10? Press T or F and enter to submit" };
var questionAnswer = new string[] { "t", "1", "4", "f", "2", "3", "t", "1", "2", "f" };//creating array of correct answers
var questionInput = new string[10];//create the input variables for each question to accept user input in an array
var questionStatus = new string[] { "n", "n", "n", "n", "n", "n", "n", "n", "n", "n" };//create question status array to track if the question was answered correctly the first time
var questionType = new string[] { "tf", "mc", "mc", "tf", "mc", "mc", "tf", "mc", "mc", "tf" };//define the question types (tf or mc) in an array
var questionIteration = 0;//set the question iteration globally
var questionCount = 0;//set the question count globally
var trueFalseCount = 0; var multipleChoiceCount = 0; //sets the true/false counter and multiple choice counters to zero
//start the quiz - display each question, in order starting at question 1, and accept the user input for that question, then display the user input and move to the next question
Console.Clear();//clear the screen
for (questionIteration = 1; questionIteration < 3; questionIteration++)//set up a for loop to run 2 iterations of the questions
{
questionCount = 0;
if (questionIteration == 2)
{
Console.WriteLine("Second attempt. Previously incorrectly answered questions will be displayed again. Good luck!");
}
foreach (string question in questions)//foreach loop to handle each question and the answer input by the user, plus logic to test for invalid and correct/incorrect answers
{
if (questionStatus[questionCount] == "n")//first attempt at the question
{
Console.WriteLine(question); //displays the question, starting with question 1
if (questionType[questionCount] == "tf")//logic for true/false questions
{
questionInput[questionCount] = InvalidTrueFalseEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
questionInput[questionCount] = CheckForCorrectAnswer();//checks for correct answer
}
else if (questionType[questionCount] == "mc")//logic for multiple choice questions
{
questionInput[questionCount] = InvalidMultipleChoiceEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
questionInput[questionCount] = CheckForCorrectAnswer();//checks for correct answer
}
Console.Clear();//clear the screen
}
//else if (questionStatus[questionCount] == "y")
//{
//Console.WriteLine("Second attempt!");//tell the user it's their second attempt
//}
questionCount++;//increment the question counter for the next iteration
}
}
Console.WriteLine("Test Complete!");//tells user the test is over
Console.WriteLine("You answered " + trueFalseCount + " T/F questions correctly and " + multipleChoiceCount + " multiple choice questions correctly.");//tells user their performance
Console.WriteLine("You missed " + (4 - trueFalseCount) + " T/F questions and " + (6 - multipleChoiceCount) + " multiple choice questions.");
Thread.Sleep(5000); Console.Clear();//clear the screen
string InvalidMultipleChoiceEntry()//this method will check multiple choice answers to ensure they are a number 1-4
{
var answer = Console.ReadLine();
if (answer != "1" && answer != "2" && answer != "3" && answer != "4")//did the user input a numner between 1 and 4?
{
Console.WriteLine("Invalid answer type (not 1-4), try again.");//if not, try again until they do!
return InvalidMultipleChoiceEntry();
}
return answer;
}
string InvalidTrueFalseEntry()//this method will check true/false answers to ensure input was a "t" or an "f"
{
var answer = Console.ReadLine();//get the user input
if (answer != "t" && answer != "f")//did the user input a t or f?
{
Console.WriteLine("Invalid answer type (not t or f), try again.");//if not, try again until they do!
return InvalidTrueFalseEntry();
}
return answer;
}
string CheckForCorrectAnswer()//this method will check the answer to see if it is correct or not, if the entry was valid for the questionType
{
var answer = questionInput[questionCount];
if (answer != questionAnswer[questionCount])//tests for incorrect answer given from a valid input by question type
{
Console.WriteLine("Your answer was: " + questionInput[questionCount]); //displays the answer chosen by the user
Console.WriteLine("Sorry, wrong answer. :-(");
Thread.Sleep(2000);
questionStatus[questionCount] = "n";//make sure the question status reflects "n"
return answer;
}
else //it must be correct, since we've checked against invalid and incorrect answers!
{
Console.WriteLine("Your answer was: " + questionInput[questionCount]); //displays the answer chosen by the user
Console.WriteLine("Correct answer!");
questionStatus[questionCount] = "y";
if (questionType[questionCount] == "tf")
{
trueFalseCount++;//increment the true/false counter to total the number answered correctly
}
else
{
multipleChoiceCount++;//increment the multiple choice counter to total the number answered correctly
}
Thread.Sleep(2000);
return answer;
}
}
}
}
}
Question
(with question, answer and type). Then redo this using a single array of them. Then remove the type field (which should not be a string but an enum) and use derived classes (if you learned about them). Then abstract input logic away into a separate function. Side note: remove thoseThread.Sleep()
...just leave the text on the screen withoutClear()
as a favor for your users! \$\endgroup\$