Intro InterFaces Terje

This commit is contained in:
Geir Okkenhaug Jerstad 2025-01-06 12:39:34 +01:00
parent f8a40750b8
commit 9f4483e85f
3 changed files with 70 additions and 2 deletions

View file

@ -0,0 +1,27 @@
namespace IntroInterface;
public class MultipleChoiceQuestions
{
private readonly string _question;
private readonly string[] _answers;
private readonly int _correctAnswerNo;
public MultipleChoiceQuestions(string question, int correctAnswerNo, params string[] answers)
{
_question = question;
_correctAnswerNo = correctAnswerNo;
_answers = answers;
}
public bool Run()
{
Console.WriteLine(_question);
Console.WriteLine("Svaralternativer: ");
for (var i = 0; i < _answers.Length; i++)
{
var answerNo = i + 1;
var answer = _answers[i];
Console.WriteLine(answerNo + ": " + answer);
}
}
}

View file

@ -1,3 +1,24 @@
// See https://aka.ms/new-console-template for more information
using IntroInterface;
Console.WriteLine("Hello, World!");
var questions = new ???[]
{
new SimpleAnswerQuestion("Hva er 2+2?", "4"),
new MultipleChoiceQuestions("Hva er hovedstaden i Norge?", 3,"Stavern", "Larvik","Oslo"),
};
var points = 0;
foreach (var question in questions)
{
var isCorrect = question.Run();
if (isCorrect)
{
Console.WriteLine("Riktig!");
points++;
}
else
{
Console.WriteLine("Feil! :-(");
}
}
Console.WriteLine($"Du fikk {points} poeng.");

View file

@ -0,0 +1,20 @@
namespace IntroInterface;
public class SimpleAnswerQuestion
{
private readonly string _question;
private readonly string _correctanswer;
public SimpleAnswerQuestion(string question, string correctanswer)
{
_correctanswer = correctanswer;
_question = question;
}
public bool Run()
{
Console.Write(_question + " ");
var answer = Console.ReadLine();
return answer == _correctanswer;
}
}