Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace Omkring_loop_til_i_morgen
- {
- class Program
- {
- static void Main(string[] args)
- {
- WhileLoop();
- DoWhileLoop();
- ForLoop();
- }
- #region While
- public static void WhileLoop()
- {
- Random numberGenerator = new Random();
- bool run = false;
- int randomNumber;
- Console.ReadLine();
- //Eksempel nr. 1
- while (run)
- {
- randomNumber = numberGenerator.Next(1, 1000);
- Console.WriteLine(randomNumber);
- if (randomNumber == 78)
- {
- Console.WriteLine("Du har nu ramt det hellige nummer!");
- run = false;
- }
- }
- //Eksempel nr. 1 slut
- //-----------------------------------------------------------------------------------
- //Eksempel nr. 2
- string[] starWarsArray = { "Obi-Wan:", "Hello There.\n", "General Grevious:", "General", "kenobi..." };
- int index = 0;
- while (index < starWarsArray.Length)
- {
- Console.WriteLine(starWarsArray[index]);
- index++;
- }
- Console.Write("\nTillykke! Du har nu skrevet alle værdierne i dit array, som har en længde på: ");
- Console.WriteLine(starWarsArray.Length);
- }
- #endregion
- #region DoWhile
- public static void DoWhileLoop()
- {
- int playerHp = 80;
- int goblinHp = 50;
- Random playerDmgRoll = new Random();
- Random goblinDmgRoll = new Random();
- Console.WriteLine("A wild aggresive goblin appears! \nYou choose to attack it.\n");
- Console.WriteLine($"You start with {playerHp} health points");
- Console.WriteLine($"The goblin starts with {goblinHp} health points \n");
- do
- {
- int playerDmg = playerDmgRoll.Next(6, 10);
- int goblinDmg = goblinDmgRoll.Next(3, 7);
- Console.ReadLine();
- Console.Clear();
- Thread.Sleep(175);
- goblinHp -= playerDmg;
- Console.WriteLine($"You attack the goblin and take away {playerDmg} of it's health points!\nIt's health points are now at {goblinHp}\n");
- playerHp -= goblinDmg;
- Console.WriteLine($"But it retaliates, and takes {goblinDmg} health points from you!\nYour health points are now at {playerHp}\n");
- } while (playerHp > 0 && goblinHp > 0 );
- if (playerHp <= 0)
- {
- Console.WriteLine("du tabte desværre!");
- }
- else if (goblinHp <= 0)
- {
- Console.WriteLine("Du vandt fandme!");
- }
- }
- #endregion
- #region For
- public static void ForLoop()
- {
- for (int i = 0; i < 100; i++)
- {
- Console.ReadLine();
- if (i == 0)
- {
- Console.WriteLine(i);
- }
- else if (i % 3 == 0 && i % 5 == 0)
- {
- Console.WriteLine("FizzBuzz");
- }
- else if (i % 3 == 0)
- {
- Console.WriteLine("Fizz");
- }
- else if (i % 5 == 0)
- {
- Console.WriteLine("Buzz");
- }
- else
- {
- Console.WriteLine(i);
- }
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement