Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package folder1;
  2.  
  3. public class iUebung {
  4. //beginn methode
  5. static int func(int n) { //ohne static nicht zugreifbar, es wird eine beliebige zahl in n eingesetzt, zB 4
  6. if (n == 0) { //wird überprüft ob n(4) == 0 ist
  7. return 1; //geht weiter zu else da n(4) nicht 0 ist
  8. }
  9.  
  10. else {
  11. return n * func(n - 1); //hier wird ausgerechnet, siehe !!! 1. unten , diese Schleife wiederholt sich bis n == 0 ist
  12. }
  13.  
  14. }
  15. //ende
  16. public static void main(String[] args) {
  17.  
  18. System.out.println(func(4)); //hier wird die methode benutzt und ausgegeben
  19.  
  20. }
  21.  
  22. }
  23.  
  24. /*
  25.  
  26. !!! 1.
  27.  
  28. Rechnung wenn man 4 einsetzt:
  29.  
  30. "4" * func (4-1)
  31. 4 * func (3)
  32. n ist jetzt 3
  33.  
  34. "3" * func (3-1)
  35. 3 * func (2)
  36. n ist jetzt 2
  37.  
  38. "2" * func (2-1)
  39. 2 * func (1)
  40. n ist jetzt 1
  41.  
  42. "1" * func (1-1)
  43. 1 * func (0)
  44. n ist jetzt 0
  45.  
  46. ERGEBNIS: "4" * "3" * "2" * "1" = 24
  47. ----------------------------------------
  48.  
  49. Rechnung wenn man 3 einsetzt:
  50.  
  51. "3" * func (3-1)
  52. 3 * func (2)
  53. n ist jetzt 2
  54.  
  55. "2" * func (2-1)
  56. 2 * func (1)
  57. n ist jetzt 1
  58.  
  59. "1" * func (1-1)
  60. 1 * func (0)
  61. n ist jetzt 0
  62.  
  63. ERGEBNIS: "3" * "2" * "1" = 6
  64. ----------------------------------------
  65.  
  66. Rechnung wenn man 2 einsetzt:
  67.  
  68. "2" * func (2-1)
  69. 2 * func (1)
  70. n ist jetzt 1
  71.  
  72. "1" * func (1-1)
  73. 1 * func (0)
  74. n ist jetzt 0
  75.  
  76. ERGEBNIS: "2" * "1" = 2
  77. ----------------------------------------
  78.  
  79. Rechnung wenn man 1 einsetzt:
  80.  
  81. "1" * func (1-1)
  82. 1 * func (0)
  83. n ist jetzt 0
  84.  
  85. ERGEBNIS: "1" = 1
  86. */
  87.  
  88.  
  89.  
  90.  
  91. ZWEITE AUFGABE:
  92.  
  93. package folder1;
  94.  
  95. public class iUebung2 {
  96.  
  97. //beginn methode
  98. static int meh(int a) { //beliebige zahl für a einsetzen, zb 5 , wird durch static erreichbar gemacht
  99. int b = 0;
  100. while (a-- > 0) { //solange 5 grösser als 0 ist, wird sich die schleife wiederholen und je durchgang wird 5-1, auch beim ersten
  101. b += a; //a-- wird zu b hinzugerechnet 4,3,2,1 = 10
  102. System.out.println(b);
  103. }
  104. return b; //b wird zurückgegeben
  105. }
  106. //ende
  107. public static void main(String[] args) {
  108.  
  109. System.out.println("Endergebnis ist " + meh(5));
  110.  
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement