fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. namespace DefaultProject
  7. {
  8. public class Test
  9. {
  10. public IList<Question> Questions { get; set; }
  11. }
  12.  
  13. public class Question
  14. {
  15. public string Text { get; set; }
  16. public IList<Answer> Answers { get; set; }
  17. }
  18.  
  19. public class Answer
  20. {
  21. public string Text { get; set; }
  22. public bool IsRight { get; set; }
  23. }
  24.  
  25.  
  26. public class Program {
  27. public static Test CreateTest()
  28. {
  29. return new Test
  30. {
  31. Questions = new List<Question>
  32. {
  33. new Question
  34. {
  35. Text = "Какая команда выводит текст в консоль?",
  36. Answers = new List<Answer>
  37. {
  38. new Answer { Text = "if/else" },
  39. new Answer { Text = "System.out.println();" },
  40. new Answer { Text = "Console.WriteLine();", IsRight = true },
  41. new Answer { Text = "int x = 10;" }
  42. }
  43. },
  44. new Question
  45. {
  46. Text = "Какой тип из списка целочисленный?",
  47. Answers = new List<Answer>
  48. {
  49. new Answer { Text = "char" },
  50. new Answer { Text = "int", IsRight = true },
  51. new Answer { Text = "float" },
  52. new Answer { Text = "double" }
  53. }
  54. }
  55. //,...
  56. }
  57. };
  58. }
  59.  
  60. public static void Main()
  61. {
  62. Console.WriteLine("Всем привет! И это моя первая мини-программа для проверки ваших знаний языка программирования C#");
  63. string input;
  64.  
  65. do
  66. {
  67. Console.WriteLine("Если готовы пройти тест напишите Go, а если хотите выйти напишите Exit.");
  68. input = Console.ReadLine();
  69.  
  70. if (input.Equals("Go", StringComparison.InvariantCultureIgnoreCase))
  71. {
  72. var test = CreateTest();
  73. PlayGame(test);
  74. }
  75. } while ( !input.Equals("Go", StringComparison.InvariantCultureIgnoreCase)
  76. && !input.Equals("Exit", StringComparison.InvariantCultureIgnoreCase));
  77.  
  78. Console.WriteLine("Для выхода нажмите любую клавишу...");
  79. Console.ReadKey();
  80. }
  81.  
  82. private static void PlayGame(Test test)
  83. {
  84. var mistakes = new List<int>();
  85. for (int i = 0; i < test.Questions.Count; ++i)
  86. {
  87. var question = test.Questions[i];
  88. PrintQuestion(question, i);
  89.  
  90. int choice = GetUserChoice(question.Answers.Count);
  91.  
  92. if (!IsCorrectAnswer(choice, question.Answers))
  93. {
  94. mistakes.Add(i + 1);
  95. }
  96. }
  97.  
  98. CalculateResults(test.Questions.Count, mistakes);
  99. }
  100.  
  101. private static void PrintQuestion(Question question, int questionIndex)
  102. {
  103. Console.WriteLine($"{questionIndex + 1}. {question.Text}");
  104.  
  105. for (int i = 0; i < question.Answers.Count; ++i)
  106. {
  107. Console.WriteLine($"\t{i + 1}. {question.Answers[i].Text}");
  108. }
  109. }
  110.  
  111. private static int GetUserChoice(int answersCount)
  112. {
  113. bool correctInput = false;
  114. int choice = -1;
  115. do
  116. {
  117. try
  118. {
  119. Console.WriteLine("Введите номер ответа:");
  120. string input = Console.ReadLine();
  121. choice = Convert.ToInt32(input);
  122. if (choice > 0 && choice <= answersCount) correctInput = true;
  123. }
  124. catch (Exception ex)
  125. {
  126. Console.WriteLine("Неправильный формат ввода");
  127. }
  128. } while (!correctInput);
  129.  
  130. return choice;
  131. }
  132.  
  133. private static bool IsCorrectAnswer(int choice, IList<Answer> answers)
  134. {
  135. var rightAnswers = answers.Where(x => x.IsRight);
  136. int chosenAnswerIndex = choice - 1;
  137. return rightAnswers.Any(x => answers.IndexOf(x) == chosenAnswerIndex);
  138. }
  139.  
  140. private static void CalculateResults(int questionsCount, IList<int> mistakes)
  141. {
  142. if (mistakes.Count == 0)
  143. {
  144. Console.WriteLine("Вы на все вопросы ответили правильно! Тест пройден на 100%! ");
  145. }
  146. else
  147. {
  148. string invalidQuestionsString = string.Join(", ", mistakes.Select(x => $"#{x}"));
  149. int progressPercentage = 100 - (int)Math.Ceiling(100 * (double)mistakes.Count / questionsCount);
  150. Console.WriteLine($"Среди ваших ответов есть неправильные ({mistakes.Count}: {invalidQuestionsString})). Тест пройден на {progressPercentage}% ");
  151. }
  152. }
  153. }
  154. }
Success #stdin #stdout 0.01s 132544KB
stdin
Go
3
1
stdout
Всем привет! И это моя первая мини-программа для проверки ваших знаний языка программирования C#
Если готовы пройти тест напишите Go, а если хотите выйти напишите Exit.
1. Какая команда выводит текст в консоль?
	1. if/else
	2. System.out.println();
	3. Console.WriteLine();
	4. int x = 10;
Введите номер ответа:
2. Какой тип из списка целочисленный?
	1. char
	2. int
	3. float
	4. double
Введите номер ответа:
Среди ваших ответов есть неправильные (1: #2)). Тест пройден на 50% 
Для выхода нажмите любую клавишу...