Bara svar ifrån vår fantastiska lärare! Inget som jag har knackat själv. Mest som backup för min del men om någon annan kanske har nytta av det. Tar inget ansvar för stavfel eller krachade datorer.

Hangman

string? word = string.Empty;

while (word == null || word.Length == 0)
{
    Console.Write("Enter word ");
    word = Console.ReadLine();
    Console.WriteLine();
}

string currenGuess = string.Empty;

while (currenGuess.Length < word.Length)
{
    currenGuess += "-";

}

bool won = false;
int lives = 5;
while (!won && lives > 0)
{
    Console.Clear();
    Console.WriteLine(currenGuess);
    Console.WriteLine("Guesses left: " + lives);

    string? input = string.Empty;

    while (input?.Length != 1 && (input?.Length != 1 && input.Length != word?.Length))
    {
        Console.Write("Please guess a letter or the whole word: ");
        input = Console.ReadLine();
        Console.WriteLine();
    }

    bool foundCorrect = false;
    if (word == input)
    {
        won = true;
    }
    else if (input.Length == 1)
    {
        for (int i = 0; i < word.Length; i++)
        {
            if (word[i].ToString() == input)
                {
                foundCorrect = true;
                currenGuess = currenGuess.Remove(i, 1).Insert(i, input);
                }
        }
    }
    if (!foundCorrect && !won)
    {
        lives--;
    }
    if (currenGuess == word)
    {
        won = true;
    }
}
if (won)
{
    Console.WriteLine("You won with " + lives + " lives left");
    Console.WriteLine("your answer so far was " + currenGuess);
}
else
{
    Console.WriteLine("You lose the word was: " + word);
    Console.WriteLine("Your answer so far was " + currenGuess);
}

Lucky Skyscraper

int realFloor = 14;
int FakeFloors = 0;

for (int count = 1; count <= realFloor + FakeFloors; count++)
{
    if (count % 10 == 4)
    {
        FakeFloors++;
    }
    if (count % 100 == 13)
    {
        FakeFloors++;
    }
}

Console.WriteLine("Your floor is: " + (realFloor + FakeFloors));

Klockan

for (int hour = 0; hour < 24; hour++)
{
    for (int minute = 0; minute < 60; minute++)
    {
        for (int second = 0; second < 60; second++)
        {
            Console.WriteLine(formatTime(hour) + ":" + formatTime(minute) + ":" + formatTime(second));
        }
    }
}

string formatTime(int number)
{
    if (number <= 9)
    {
        return "0" + number;
    }
    else
    {
        return number.ToString();
    }
}

Stuff // tribute Jimmy i discord

// Läsa input
var input = Console.ReadLine();
Console.WriteLine("Your input was: " + input);


// Byta ut bokstäver
var word = "hejdå";
word = word.Remove(3, 1).Insert(3, "a");
Console.WriteLine(word);


// Loopa igenom allting i en array
var everything = "a word";
foreach (var item in word)
{
    Console.Write("_" + item);
}
Console.WriteLine();


// Booleans
var answer = false;
if (!answer)
{
    Console.WriteLine("Answer if false");
}

answer = true;

if (answer)
{
    Console.WriteLine("Answer if true");
}

// Hämta data ifrån strings
Console.WriteLine("The length of the word: " + input + "is: " + input.Length);

// String manipulation
Console.WriteLine("My input word, when CAPS is: " + input.ToUpper());
Console.WriteLine("My input word, when lower is: " + input.ToLower());
Scroll to Top