Advertisement
Guest User

skool1

a guest
Sep 15th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. static void Main(string[] args)
  2.         {
  3.             Console.WriteLine("This is a file-management based quiz application.");
  4.             Console.WriteLine("Press ENTER to begin.");
  5.             Console.ReadKey();
  6.             Console.Clear();
  7.             Console.WriteLine();
  8.             Console.WriteLine("Please select which quiz you'd like to answer: ");
  9.             Console.WriteLine();
  10.             DirectoryInfo dinfo = new DirectoryInfo(@"D:\Documents\Computing\Projects\School apps\Quiz project");
  11.             FileInfo[] quizzes = dinfo.GetFiles("*.txt");
  12.             int counter = 1;
  13.             foreach (FileInfo quiz in quizzes)
  14.             {
  15.                 int index = quiz.Name.IndexOf(".");
  16.                 string fileName = quiz.Name.Substring(0, index);
  17.                 Console.WriteLine("[" + counter + "] " + fileName);
  18.                 counter = counter + 1;
  19.             }
  20.             Console.WriteLine();
  21.             Console.Write("Selection > ");
  22.             int quizSelected = int.Parse(Console.ReadLine());
  23.             Console.Clear();
  24.             string quizSelectedName = quizzes[quizSelected - 1].Name.ToString().Substring(0, quizzes[quizSelected - 1].Name.ToString().IndexOf('.'));
  25.             Console.WriteLine();
  26.             Console.WriteLine("Starting your selected quiz: " + quizSelectedName + "!");
  27.             string quizDir = @"D:\Documents\Computing\Projects\School apps\Quiz project\" + quizSelectedName + ".txt";
  28.             Console.WriteLine("Press ENTER to start!");
  29.             Console.ReadKey();
  30.  
  31.             // Formatting guide.
  32.             // Files are formatted as follows:
  33.             //
  34.             // What is the answer to question 1?;Answer1
  35.             // What is the answer to question 2?;Answer2
  36.             // What is the answer to question 3?;Answer3
  37.  
  38.             StreamReader quizReader = new StreamReader(quizDir);
  39.             int lines = File.ReadAllLines(quizDir).Count();
  40.             int score = 0;
  41.             for (int i = 0; i < lines; i++)
  42.             {
  43.                 Console.Clear();
  44.                 string thisLine = quizReader.ReadLine();
  45.                 string thisQuestion = thisLine.Substring(0, thisLine.IndexOf(";"));
  46.                 string thisAnswer = thisLine.Substring(thisLine.IndexOf(";") + 1).ToLower();
  47.                 Console.WriteLine();
  48.                 Console.WriteLine("Current Score: {0}", score);
  49.                 Console.WriteLine("You've answered " + i + " questions so far.");
  50.                 Console.WriteLine();
  51.                 Console.WriteLine("------ QUESTION " + (i + 1) + " ------");
  52.                 Console.WriteLine();
  53.                 Console.WriteLine("Q > " + thisQuestion);
  54.                 Console.Write("A > ");
  55.                 string givenAnswer = Console.ReadLine().ToLower();
  56.                 if (givenAnswer == thisAnswer)
  57.                 {
  58.                     // Correct!
  59.                     score = score + 1;
  60.                     Console.WriteLine();
  61.                     Console.WriteLine("Answer correct! :)");
  62.                 }
  63.                 else
  64.                 {
  65.                     // Incorrect!
  66.                     Console.WriteLine();
  67.                     Console.WriteLine("Answer incorrect, sorry! :(");
  68.                 }
  69.                 Console.ReadKey();
  70.             }
  71.             Console.Clear();
  72.             Console.WriteLine();
  73.             Console.WriteLine("Quiz \"" + quizSelectedName + "\" completed!");
  74.             string Percentage = ((score/lines)*100).ToString("0.0");
  75.             Console.WriteLine("You scored " + score + "/" + lines + " ("+Percentage+"%)!");
  76.             Console.WriteLine();
  77.             // High score saving etc.
  78.             // Format:
  79.             //
  80.             // Quiz name; Person1,Score
  81.             // Quiz name; Person2,Score
  82.  
  83.             // End of program.
  84.             Console.WriteLine("Application completed. Press ENTER to exit.");
  85.             Console.ReadKey();
  86.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement