using System;
using System.Collections.Generic;
using System.Linq;
namespace DefaultProject
{
public class Test
{
public IList<Question> Questions { get; set; }
}
public class Question
{
public string Text { get; set; }
public IList<Answer> Answers { get; set; }
}
public class Answer
{
public string Text { get; set; }
public bool IsRight { get; set; }
}
public class Program {
public static Test CreateTest()
{
return new Test
{
Questions = new List<Question>
{
new Question
{
Text = "Какая команда выводит текст в консоль?",
Answers = new List<Answer>
{
new Answer { Text = "if/else" },
new Answer { Text = "System.out.println();" },
new Answer { Text = "Console.WriteLine();", IsRight = true },
new Answer { Text = "int x = 10;" }
}
},
new Question
{
Text = "Какой тип из списка целочисленный?",
Answers = new List<Answer>
{
new Answer { Text = "char" },
new Answer { Text = "int", IsRight = true },
new Answer { Text = "float" },
new Answer { Text = "double" }
}
}
//,...
}
};
}
public static void Main()
{
Console.WriteLine("Всем привет! И это моя первая мини-программа для проверки ваших знаний языка программирования C#");
string input;
do
{
Console.WriteLine("Если готовы пройти тест напишите Go, а если хотите выйти напишите Exit.");
input = Console.ReadLine();
if (input.Equals("Go", StringComparison.InvariantCultureIgnoreCase))
{
var test = CreateTest();
PlayGame(test);
}
} while ( !input.Equals("Go", StringComparison.InvariantCultureIgnoreCase)
&& !input.Equals("Exit", StringComparison.InvariantCultureIgnoreCase));
Console.WriteLine("Для выхода нажмите любую клавишу...");
Console.ReadKey();
}
private static void PlayGame(Test test)
{
var mistakes = new List<int>();
for (int i = 0; i < test.Questions.Count; ++i)
{
var question = test.Questions[i];
PrintQuestion(question, i);
int choice = GetUserChoice(question.Answers.Count);
if (!IsCorrectAnswer(choice, question.Answers))
{
mistakes.Add(i + 1);
}
}
CalculateResults(test.Questions.Count, mistakes);
}
private static void PrintQuestion(Question question, int questionIndex)
{
Console.WriteLine($"{questionIndex + 1}. {question.Text}");
for (int i = 0; i < question.Answers.Count; ++i)
{
Console.WriteLine($"\t{i + 1}. {question.Answers[i].Text}");
}
}
private static int GetUserChoice(int answersCount)
{
bool correctInput = false;
int choice = -1;
do
{
try
{
Console.WriteLine("Введите номер ответа:");
string input = Console.ReadLine();
choice = Convert.ToInt32(input);
if (choice > 0 && choice <= answersCount) correctInput = true;
}
catch (Exception ex)
{
Console.WriteLine("Неправильный формат ввода");
}
} while (!correctInput);
return choice;
}
private static bool IsCorrectAnswer(int choice, IList<Answer> answers)
{
var rightAnswers = answers.Where(x => x.IsRight);
int chosenAnswerIndex = choice - 1;
return rightAnswers.Any(x => answers.IndexOf(x) == chosenAnswerIndex);
}
private static void CalculateResults(int questionsCount, IList<int> mistakes)
{
if (mistakes.Count == 0)
{
Console.WriteLine("Вы на все вопросы ответили правильно! Тест пройден на 100%! ");
}
else
{
string invalidQuestionsString = string.Join(", ", mistakes.Select(x => $"#{x}"));
int progressPercentage = 100 - (int)Math.Ceiling(100 * (double)mistakes.Count / questionsCount);
Console.WriteLine($"Среди ваших ответов есть неправильные ({mistakes.Count}: {invalidQuestionsString})). Тест пройден на {progressPercentage}% ");
}
}
}
}