Advertisement
Guest User

uppgift2

a guest
Nov 13th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. int antal;
  2.  
  3. while (true)
  4. {
  5. Console.Write("Hur många heltal vill du skriva?: ");
  6.  
  7. try
  8. {
  9. antal = int.Parse(Console.ReadLine());
  10.  
  11. if (antal > 0)
  12. {
  13. break;
  14. }
  15. else
  16. {
  17. throw new Exception("Talet är mindre än 1.");
  18. }
  19. }
  20. catch (Exception e)
  21. {
  22. Console.Clear();
  23. Console.WriteLine(e.Message + " Vänligen försök igen...");
  24. continue;
  25. }
  26. }
  27.  
  28. int[] array = new int[antal];
  29.  
  30. for (int i = 0; i < array.Length; i++)
  31. {
  32. while (true)
  33. {
  34. Console.Write($"Tal {i + 1}: ");
  35.  
  36. try
  37. {
  38. array[i] = int.Parse(Console.ReadLine());
  39. break;
  40. }
  41. catch (Exception e)
  42. {
  43. Console.Clear();
  44. Console.WriteLine(e.Message + " Vänligen försök igen...");
  45. continue;
  46. }
  47. }
  48. }
  49.  
  50. for (int i = array.Length - 1; i >= 0; i--)
  51. {
  52. Console.Write(array[i] + " ");
  53. }
  54.  
  55. Console.WriteLine();
  56. Console.ReadKey();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement