Oppgave Krokodillespillet

This commit is contained in:
Geir Okkenhaug Jerstad 2024-11-18 14:03:05 +01:00
parent 8a7cf65aa4
commit 63ba67cfc6
3 changed files with 110 additions and 0 deletions

View file

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Krokodillespillet", "Krokodillespillet\Krokodillespillet.csproj", "{7A64F3FF-CB86-427B-A77E-320415991537}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7A64F3FF-CB86-427B-A77E-320415991537}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A64F3FF-CB86-427B-A77E-320415991537}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A64F3FF-CB86-427B-A77E-320415991537}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A64F3FF-CB86-427B-A77E-320415991537}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,84 @@
using System;
namespace KrokodilleSpillet
{
class Program
{
static bool running = true;
static string rightAnswer;
static void Main(string[] args)
{
ProgramLoop();
}
static void ProgramLoop()
{
while (running == true)
{
var rand1 = new Random();
var rand2 = new Random();
int randomNumber = rand1.Next(0,11);
int randomNumer2 = rand2.Next(0,11);
Console.WriteLine("Skriv inn riktig > < eller =. Alle andre tegn avslutter programmet");
Console.WriteLine($"Hva er riktig tegn i utrykket {randomNumber}_{randomNumer2}");
string answer = Console.ReadLine();
GetRightAnswer(randomNumber, randomNumer2);
CheckAnswer(answer);
}
}
static string GetRightAnswer(int rand1, int rand2)
{
if (rand1 == rand2)
{
rightAnswer = "=";
} else if (rand1 > rand2)
{
rightAnswer = ">";
} else if (rand1 < rand2)
{
rightAnswer = "<";
}
return rightAnswer;
}
static void CheckAnswer(string answer)
{
if (answer != "<" && answer != ">" && answer != "=")
{
Console.WriteLine("På gjensyn :-)");
running = false;
}
else if (answer == rightAnswer)
{
PrintResponse(true);
Console.Clear();
} else if(answer != rightAnswer)
{
PrintResponse(false);
Console.Clear();
}
}
static void PrintResponse(bool arg)
{
if (arg == true)
{
Console.WriteLine($"\nDet er riktig");
Console.WriteLine($"\nTrykk en tast for å prøve igjen");
Console.ReadKey();
}
else
{
Console.WriteLine($"\nDet er feil");
Console.WriteLine($"\nRiktig svar var {rightAnswer}");
Console.WriteLine($"\nTrykk en tast for å prøve igjen");
Console.ReadKey();
}
}
}
}