Intro InterFaces Terje
This commit is contained in:
parent
f8a40750b8
commit
9f4483e85f
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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.");
|
20
Emne 3/IntroInterface/IntroInterface/SimpleAnswerQuestion.cs
Normal file
20
Emne 3/IntroInterface/IntroInterface/SimpleAnswerQuestion.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue