Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace kertotaulu
  8. {
  9. class kertotaulu
  10. {
  11.  
  12.  
  13.  
  14. public static void Main(string[] args)
  15. {
  16. int rivi, sarake;
  17.  
  18. while (true)
  19. {
  20. LueRivitJaSarakkeet(out rivi, out sarake);
  21. TulostaTaulukko(LuoTaulukko(rivi, sarake));
  22. }
  23.  
  24.  
  25.  
  26.  
  27. }
  28.  
  29. public static void LueRivitJaSarakkeet(out int rivi, out int sarake)
  30. {
  31. bool huijaabottia = false;
  32.  
  33. Console.WriteLine("Anna taulun rivien määrä: ");
  34. bool success = int.TryParse(Console.ReadLine(), out rivi);
  35. if (!success || rivi <= 0)
  36. {
  37. huijaabottia = true;
  38.  
  39. }
  40. Console.WriteLine("Anna taulun sarakkeiden määrä: ");
  41. success = int.TryParse(Console.ReadLine(), out sarake);
  42. if (!success || sarake <= 0)
  43. {
  44. huijaabottia = true;
  45.  
  46. }
  47.  
  48. if(huijaabottia)
  49. System.Environment.Exit(1);
  50.  
  51. Console.Write("\n");
  52.  
  53. }
  54.  
  55. public static int[,] LuoTaulukko(int rivi, int sarake)
  56. {
  57. int[,] taulu = new int[rivi, sarake];
  58.  
  59. for (int r = 0; r < taulu.GetLength(0); r++)
  60. {
  61. for (int s = 0; s < taulu.GetLength(1); s++)
  62. {
  63. taulu[r, s] = (r + 1) * (s + 1);
  64. }
  65.  
  66. }
  67.  
  68.  
  69. return taulu;
  70. }
  71.  
  72. public static void TulostaTaulukko(int[,] taulukko)
  73. {
  74. int sarake = 1, rivi = 1;
  75. // Console.Write(" ");
  76. while(sarake <= taulukko.GetLength(1))
  77. {
  78. Console.Write("{0} ", sarake);
  79. sarake++;
  80. }
  81. Console.Write("\n");
  82. Console.Write("\n");
  83.  
  84. for (int r = 0; r < taulukko.GetLength(0); r++)
  85. {
  86. Console.Write("{0} ", rivi);
  87. rivi++;
  88. for (int s = 0; s < taulukko.GetLength(1); s++)
  89. {
  90. Console.Write("{0} ", taulukko[r, s]);
  91. }
  92. Console.Write("\n");
  93. }
  94. Console.Write("\n");
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement