Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Omkring_loop_til_i_morgen
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             WhileLoop();
  11.             DoWhileLoop();
  12.             ForLoop();
  13.         }
  14.         #region While
  15.         public static void WhileLoop()
  16.         {
  17.             Random numberGenerator = new Random();
  18.             bool run = false;
  19.             int randomNumber;
  20.  
  21.             Console.ReadLine();
  22.  
  23.  
  24.  
  25.             //Eksempel nr. 1
  26.             while (run)
  27.             {
  28.                 randomNumber = numberGenerator.Next(1, 1000);
  29.                 Console.WriteLine(randomNumber);
  30.  
  31.                 if (randomNumber == 78)
  32.                 {
  33.                     Console.WriteLine("Du har nu ramt det hellige nummer!");
  34.                     run = false;
  35.                 }
  36.             }
  37.             //Eksempel nr. 1 slut
  38.             //-----------------------------------------------------------------------------------
  39.             //Eksempel nr. 2
  40.  
  41.             string[] starWarsArray = { "Obi-Wan:", "Hello There.\n", "General Grevious:", "General", "kenobi..." };
  42.             int index = 0;
  43.  
  44.             while (index < starWarsArray.Length)
  45.             {
  46.                 Console.WriteLine(starWarsArray[index]);
  47.                 index++;
  48.             }
  49.             Console.Write("\nTillykke! Du har nu skrevet alle værdierne i dit array, som har en længde på: ");
  50.             Console.WriteLine(starWarsArray.Length);
  51.         }
  52.         #endregion
  53.         #region DoWhile
  54.         public static void DoWhileLoop()
  55.         {
  56.             int playerHp = 80;
  57.             int goblinHp = 50;
  58.             Random playerDmgRoll = new Random();
  59.             Random goblinDmgRoll = new Random();
  60.  
  61.  
  62.             Console.WriteLine("A wild aggresive goblin appears! \nYou choose to attack it.\n");
  63.             Console.WriteLine($"You start with {playerHp} health points");
  64.             Console.WriteLine($"The goblin starts with {goblinHp} health points \n");
  65.  
  66.  
  67.             do
  68.             {
  69.                 int playerDmg = playerDmgRoll.Next(6, 10);
  70.                 int goblinDmg = goblinDmgRoll.Next(3, 7);
  71.                 Console.ReadLine();
  72.                 Console.Clear();
  73.                 Thread.Sleep(175);
  74.  
  75.                 goblinHp -= playerDmg;
  76.                 Console.WriteLine($"You attack the goblin and take away {playerDmg} of it's health points!\nIt's health points are now at {goblinHp}\n");
  77.  
  78.  
  79.                 playerHp -= goblinDmg;
  80.                 Console.WriteLine($"But it retaliates, and takes {goblinDmg} health points from you!\nYour health points are now at {playerHp}\n");
  81.  
  82.  
  83.             } while (playerHp > 0 && goblinHp > 0 );
  84.            
  85.             if (playerHp <= 0)
  86.             {
  87.                 Console.WriteLine("du tabte desværre!");
  88.             }
  89.            
  90.             else if (goblinHp <= 0)
  91.             {
  92.                 Console.WriteLine("Du vandt fandme!");
  93.             }
  94.         }
  95.         #endregion
  96.         #region For
  97.         public static void ForLoop()
  98.         {
  99.  
  100.             for (int i = 0; i < 100; i++)
  101.             {
  102.                 Console.ReadLine();
  103.                 if (i == 0)
  104.                 {
  105.                     Console.WriteLine(i);
  106.                 }
  107.                 else if (i % 3 == 0 && i % 5 == 0)
  108.                 {
  109.                     Console.WriteLine("FizzBuzz");
  110.                 }
  111.                 else if (i % 3 == 0)
  112.                 {
  113.                     Console.WriteLine("Fizz");
  114.                    
  115.                 }
  116.                 else if (i % 5 == 0)
  117.                 {
  118.                     Console.WriteLine("Buzz");
  119.                 }
  120.                 else
  121.                 {
  122.                     Console.WriteLine(i);
  123.                 }
  124.             }
  125.         }
  126.         #endregion
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement