Funktionale Programmierung

Burntime
Hallo,

ich will/muss auf funktionalem Wege ( ohne Zuweisungen und schleifen) n verschiedene zahlen addieren. Weiß jemand wie ich das anstellen soll?

ich bekomme von meinen zufallsgenerator* nen int wert zurück diesen soll ich dan n mal addieren


*zufallsgenerator:
code:
1:
2:
3:
4:
public static int wuerfel() {
int wuerfel = (int) (Math.random() * 6 + 1);
return wuerfel;
}
Prophet
mein vorschlag wäre ein rekursiver funktionsaufruf:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
public static int add(int source, int addend, int counter, int end)
{
   if(counter <= end)
      add(source+addend, addend, counter+1, end);
   else
      return (source+addend);
}


source = basis startwert
addend = summand der aufaddiert wird
counter = zähler startwert
end = zähler endwert

im prinzip nichts anderes als eine schleife nur auf funktionaler basis und ohne zuweisungen smile

EDIT: als addend kanns ja einfach wuerfel() benutzen. (habs noich nicht getestet!)
Burntime
ja habe ich mir auch gedacht...

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
 public class funktional {

    /**
     * @param args
     * @author Alex Kulikov
     */
    static int n = 5;

    static int summe = 0;

    public static void main(String[] args) {
        summeVonWuerfeln(n);

    }

    private static int summeVonWuerfeln(int n) {
        if (n != 0) {
            summe = summe + wuerfel();
            n = n - 1;
            summeVonWuerfeln(n);
        } else {
            System.out.println(summe);
        }

        return 0;

    }

    public static int wuerfel() {
        int wuerfel = (int) (Math.random() * 6 + 1);
        return wuerfel;
    }

}
Prophet
bei dir besteht nur das proböem das sich würfel mitendrin ändern kann. soweit ich dich vrstanden habe soll das doch nicht passieren oder?
Burntime
was Würfel mach ist egal der soll einfach nur irgend eine Zahl ausgeben und ich soll diese n mal addieren.

und das Problem wahr halt das n mal addieren... weil man dazu ja eigentlich ne schleife bräuchte. Da ich die nicht haben darf muss ich also die Funktion immer wieder selber aufrufen.
Prophet
dann ist ja alles paletti smile
Burntime
hmm wahr leider immer noch nicht Funktional weil zuweisungen enthalten wahren...
Prophet
code:
1:
2:
3:
public static int wuerfel() { 
   return (int) (Math.random() * 6 + 1);
}
funktional

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
private static int summeVonWuerfeln(int n) {
        if (n != 0) {
            summe = summe + wuerfel();
            n = n - 1;
            summeVonWuerfeln(n);
        } else {
            System.out.println(summe);
        }

        return 0;

    }
du hättest ja auch einfach meine staattdessen nehmen können die war nämlich ohne

code:
1:
2:
3:
4:
5:
6:
7:
public static int add(int source, int addend, int counter, int end)
{
   if(counter <= end)
      return add(source+addend, addend, counter+1, end);
   else
      return (source+addend);
}


da musst du nur die richtigen parameter setzten

dann brauchst du auch keine variablen in der klasse....