Advertisement
Guest User

Untitled

a guest
May 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp76
  5. {
  6. class Program
  7. {
  8.  
  9.  
  10. static void Main(string[] args)
  11. { //Creating a list to store all the dice results, one variable to temporarely store the dice result and two counters to handle the numbers of thrown dices and number of dices left to throw.
  12. int QuantityLeft;
  13. int QuantityRolled = 0;
  14. int DiceResult;
  15. int totalsum = 0;
  16. string input;
  17. Random dice = new Random();
  18.  
  19. List<int> diceList = new List<int>();
  20.  
  21. Console.WriteLine("Patrik Johansson 19/5 2019. Tryck ned valfri tangent för att gå till tärningssimulatorn.");
  22. Console.ReadKey();
  23. Console.Clear();
  24.  
  25.  
  26. bool Run = true;
  27. while (Run)
  28. {
  29. do // The user selects how many dices to roll. Using tryparse for error handling.
  30. {
  31.  
  32.  
  33. Console.WriteLine("Ange ett tal mellan 1-5 för att välja hur många tärningar som ska rullas:");
  34. bool okey = int.TryParse(Console.ReadLine(), out QuantityLeft);
  35. }
  36. while (QuantityLeft < 1 || QuantityLeft > 5); //Checks that input is within limits (1-5)
  37.  
  38. do // Rolling dice and increses QuantityRolled.
  39. {
  40. Console.WriteLine("Tryck ned enter för att kasta tärning!");
  41. Console.ReadKey();
  42. DiceResult = dice.Next(1, 7);
  43. QuantityRolled++;
  44.  
  45. if (DiceResult == 6) // Tells the user that the dice became 6 and adding 2 more rolls to QuantityLeft.
  46. {
  47.  
  48. Console.WriteLine("Tärningen blev 6, du får 2 nya tärningsrull");
  49. QuantityLeft = QuantityLeft + 2;
  50. }
  51.  
  52. {
  53.  
  54. Console.WriteLine("Din tärning blev " + DiceResult); // Tells user the diceresult and and the total amount so far.
  55.  
  56. diceList.Add(DiceResult); // Adding diceresult to list.
  57.  
  58. int sum = 0;
  59. for (int i = 0; i < diceList.Count; i++) // Summarize the dicelist.
  60. {
  61. sum += diceList[i];
  62. }
  63.  
  64. Console.WriteLine("Summan av alla dina slag hittils är: " + sum); // Tells the user total amount.
  65. totalsum = sum;
  66. }
  67.  
  68. QuantityLeft--; // Decreases the amount of dices left.
  69. }
  70. while (QuantityLeft > 0); // Rolls dices until there is no more left.
  71.  
  72. Console.WriteLine("Totalsumman av alla tärningsslag blev: " + totalsum + " och antal rull är: " + QuantityRolled); // Tells user the total amount of the rolled dices and how many dices rolled.
  73.  
  74.  
  75. Console.WriteLine("Vill du kasta igen? Ange Ja eller Nej"); // Asking if user wants to restart or end program.
  76.  
  77. input = Console.ReadLine();
  78.  
  79. if (input.ToLower() == "ja")
  80. {
  81. Run = true;
  82. }
  83.  
  84. else
  85.  
  86. {
  87. Run = false;
  88. }
  89.  
  90. }
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement