Death420

basic C# quiz app

Feb 18th, 2020
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1.     class Program
  2.     {
  3.         static Action<dynamic> print = Console.Write;
  4.         public class Question
  5.         {
  6.             int correctAnswer;
  7.             double points;
  8.             string text;
  9.             string[] options;
  10.  
  11.             public Question(int correctAnswer, double points, string text, string[] options)
  12.             {
  13.                 this.correctAnswer = correctAnswer;
  14.                 this.points = points;
  15.                 this.text = text;
  16.                 this.options = options;
  17.             }
  18.             public double GetAnswer()
  19.             {
  20.                 print(text + "\n\n");
  21.                 for (int i = 0; i < options.Length; i++)
  22.                 {
  23.                     print($"{i + 1}: {options[i]}\n");
  24.                 }
  25.                 print("Select your answer:\n\n");
  26.                 int answer = int.Parse(Console.ReadLine());
  27.  
  28.                 if (answer == correctAnswer)
  29.                 {
  30.                     print("\nCorrect.\n\n");
  31.                     return points;
  32.  
  33.                 }
  34.                 else
  35.                 {
  36.                     print("\nWrong.\n\n");
  37.                     return 0;
  38.                 }
  39.                    
  40.             }
  41.         }
  42.         static void Main(string[] args)
  43.         {
  44.             List<Question> quiz = new List<Question>();
  45.             //correctAnswer, points, text, {options...}
  46.             quiz.Add(new Question(1, 42.0, "What's the best anime?", new[] { "DBZ", "Naruto", "Bleach"}));
  47.             quiz.Add(new Question(2, 99.0, "Should Death be admin?", new[] { "No.", "Yes."}));
  48.  
  49.             double total = 0;
  50.             foreach (var question in quiz)
  51.                 total += question.GetAnswer();
  52.  
  53.             print($"Your final score is: {total}\n");
  54.  
  55.         }
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment