Как создать экземпляры класса используя массив? - C#
Формулировка задачи:
Нужно создать объекты класса с помощью массива.
И еще вопрос, как сделать сравнение введенного пользователем ответа, если мы выводим варианты ответов рандомом ?
Решение задачи: «Как создать экземпляры класса используя массив?»
textual
Листинг программы
using System;
namespace CyberForum
{
public class Task
{
public string task { get; set; }
public int solution { get; set; }
public int[] variants { get; set; }
public Task(string task, int solution, int[] variants)
{
this.task = task;
this.solution = solution;
this.variants = variants;
}
public bool checkSolution(int guess, Task t)
{
if (guess == t.solution) return true;
return false;
}
}
class TestTast
{
static void Main(string[] args)
{
Task task1 = new Task("2+2=", 4, new int[] { 1, 2, 3, 4 });
Console.WriteLine("{0} \nВарианты: {1}", task1.task, string.Join(" | ", task1.variants));
int guess = Int32.Parse(Console.ReadLine());
bool isRight = task1.checkSolution(guess, task1);
if (isRight) Console.WriteLine("Правильно!");
else Console.WriteLine("Неправильно");
Console.ReadKey(true);
}
}
}