Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- Console.WriteLine("This is a file-management based quiz application.");
- Console.WriteLine("Press ENTER to begin.");
- Console.ReadKey();
- Console.Clear();
- Console.WriteLine();
- Console.WriteLine("Please select which quiz you'd like to answer: ");
- Console.WriteLine();
- DirectoryInfo dinfo = new DirectoryInfo(@"D:\Documents\Computing\Projects\School apps\Quiz project");
- FileInfo[] quizzes = dinfo.GetFiles("*.txt");
- int counter = 1;
- foreach (FileInfo quiz in quizzes)
- {
- int index = quiz.Name.IndexOf(".");
- string fileName = quiz.Name.Substring(0, index);
- Console.WriteLine("[" + counter + "] " + fileName);
- counter = counter + 1;
- }
- Console.WriteLine();
- Console.Write("Selection > ");
- int quizSelected = int.Parse(Console.ReadLine());
- Console.Clear();
- string quizSelectedName = quizzes[quizSelected - 1].Name.ToString().Substring(0, quizzes[quizSelected - 1].Name.ToString().IndexOf('.'));
- Console.WriteLine();
- Console.WriteLine("Starting your selected quiz: " + quizSelectedName + "!");
- string quizDir = @"D:\Documents\Computing\Projects\School apps\Quiz project\" + quizSelectedName + ".txt";
- Console.WriteLine("Press ENTER to start!");
- Console.ReadKey();
- // Formatting guide.
- // Files are formatted as follows:
- //
- // What is the answer to question 1?;Answer1
- // What is the answer to question 2?;Answer2
- // What is the answer to question 3?;Answer3
- StreamReader quizReader = new StreamReader(quizDir);
- int lines = File.ReadAllLines(quizDir).Count();
- int score = 0;
- for (int i = 0; i < lines; i++)
- {
- Console.Clear();
- string thisLine = quizReader.ReadLine();
- string thisQuestion = thisLine.Substring(0, thisLine.IndexOf(";"));
- string thisAnswer = thisLine.Substring(thisLine.IndexOf(";") + 1).ToLower();
- Console.WriteLine();
- Console.WriteLine("Current Score: {0}", score);
- Console.WriteLine("You've answered " + i + " questions so far.");
- Console.WriteLine();
- Console.WriteLine("------ QUESTION " + (i + 1) + " ------");
- Console.WriteLine();
- Console.WriteLine("Q > " + thisQuestion);
- Console.Write("A > ");
- string givenAnswer = Console.ReadLine().ToLower();
- if (givenAnswer == thisAnswer)
- {
- // Correct!
- score = score + 1;
- Console.WriteLine();
- Console.WriteLine("Answer correct! :)");
- }
- else
- {
- // Incorrect!
- Console.WriteLine();
- Console.WriteLine("Answer incorrect, sorry! :(");
- }
- Console.ReadKey();
- }
- Console.Clear();
- Console.WriteLine();
- Console.WriteLine("Quiz \"" + quizSelectedName + "\" completed!");
- string Percentage = ((score/lines)*100).ToString("0.0");
- Console.WriteLine("You scored " + score + "/" + lines + " ("+Percentage+"%)!");
- Console.WriteLine();
- // High score saving etc.
- // Format:
- //
- // Quiz name; Person1,Score
- // Quiz name; Person2,Score
- // End of program.
- Console.WriteLine("Application completed. Press ENTER to exit.");
- Console.ReadKey();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement