Rekursion-Verständnis

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Rekursion-Verständnis

    Guten Tag

    Ich bin Maschinentechnik Student und besuch nun den Java-Kurs.
    Leider stehe ich des öftern an und auch nun habe ich per Zufall mit einem Kameraden zusammen dieses Programm entworfen. Erstaundlicherweise funktioniert es sogar, doch ich habe grosse Mühe die Rekursion an sich zu verstehen. Wenn ich z.B. die Zahl 5 eingebe was macht der Computer dann genau? Also ich weiss, dass ich ein Hauptrogramm (main) geschrieben habe. Darin habe ich einen Link zu einem Unterprogramm mit dem Namen Fakultaet (x). Und dann habe ich das Unterprogramm in dem ich die gesamte Rechenarbeit programmiert habe.

    Aber genau dort happerts.
    Dort weiss, dass wenn x aussieht wie 0 dann gehe zurück ins main und gib mit die Zahl aus. Was er dann bei else macht weiss ich jedoch nicht genau.
    Vor allem wie er das ganze am Schluss zusammenbekommt und mir dann 120 ausgiebt. Ich habe ja gar keine Addition oder so programmiert.

    Vielen Dank für die Hilfe.
    Gruss Ferstl








    Das Programm lautet:

    import java.io.*;
    public class FakultaetRekursiv {

    public static void main (String[] args) {



    System.out.print ("Bitte geben Sie eine Zahl ein: ");
    int x = Console.readInt ();
    System.out.println ("Die Fakulaet lautet " + Fakultaet (x));

    System.out.println ();
    System.out.println ("bye bye");


    }

    public static int Fakultaet (int x) {

    if (x==0) {
    return 1;
    }

    else return Fakultaet (x-1)*x;
    }




    }
  • Hallo Ferstel,

    Rekursion ist am anfang immer ein wenig schwer zu verstehen. Ich versuchs mal so:

    Du hast deine Problemstellung, in deinem Fall die Fakulät,..nehmen wir mal das Beispiel "Fakultät von 3"
    Deine (rekursive) Funktion "Fakultaet" macht nun folgendes: Sie ruft sich solange selbst wieder auf bis das Problem trivial lösbar ist. Trivial lösbar ist im Falle der Fakultät: 0! Die Fakultät von 0 ist bekanntermaßen 1. Das wird durch die Zeile

    [code:1]
    if (x==0) {
    return 1;
    }
    [/code:1]

    erzielt. Wenn also x == 0 ist, wäre das ergebnis also 1. Nun willst du ja nicht das ergebnis von 0! haben sondern von 3!. Hier kommt nun dein "else-Zweig" ins Spiel. Er wird nun in jedem rekursionsschritt das Problem vereinfachen (bis es trivial lösbar ist => 0!) Um dies zu erreichen wird als ergebnis die Funktion Fakultaet von (x-1)*x errechnet. Du siehst also dass sich hier die Funktion selbst wieder aufruft und die übergebene Zahl x bei jedem Durchlauf um 1 erniedrigt, solange bis der Trivialfall erreicht ist. Danach baut er dir das Ergebnis beginnent vom trivialfall bis zu deinem eingegebenen x zusammen, das kannst du dir dann in etwa so vorstellen:

    fac (3) = fac(3*fac(2*fac(1)))

    Hoffe ich konnte dir ein wenig weiterhelfen, wenn was unklar sein sollte einfach nachfragen ;)
    mfg KC
  • Can it really be sixty-two years ago that I first saw you?

    It is truly a lifetime, I know. But as I gaze into your eyes now, it seems like only yesterday that I first saw you, in that small café in Hanover Square. (power leveling)

    From the moment I saw you smile, as you opened the door for that young mother and her newborn baby. I knew. I knew that I wanted to share the rest of my life with you.

    I still think of how foolish I must have looked, as I gazed at you, that first time. I remember watching you intently, as you took off your hat and loosely shook your short dark hair with your fingers. I felt myself becoming 1)immersed in your every detail, as you placed your hat on the table and cupped your hands around the hot cup of tea, gently blowing the steam away with your 2)pouted lips.

    From that moment, everything seemed to make perfect sense to me. The people in the café and the busy street outside all disappeared into a 3)hazy 4)blur. All I could see was you.

    All through my life I have relived that very first day. Many, many times I have sat and thought about that the first day, and how for a few 5)fleeting moments I am there, feeling again what is like to know true love for the very first time. It pleases me that I can still have those feelings now after all those years, and I know I will always have them to comfort me.

    Not even as I shook and trembled uncontrollably in the 6)trenches, did I forget your face. I would sit huddled into the wet mud, terrified, as the hails of bullets and mortars crashed down around me. I would 7)clutch my rifle tightly to my heart, and think again of that very first day we met. I would cry out in fear, as the noise of war beat down around me. But, as I thought of you and saw you smiling back at me, everything around me would be become silent, and I would be with you again for a few precious moments, far from the death and destruction. It would not be until I opened my eyes once again, that I would see and hear the 8)carnage of the war around me.

    I cannot tell you how strong my love for you was back then, when I returned to you on leave in the September, feeling 9)battered, bruised and fragile. We held each other so tight I thought we would burst. I asked you to marry me the very same day and I 10)whooped with joy when you looked deep into my eyes and said "yes" to being my bride.(wow power level)

    I`m looking at our wedding photo now, the one on our dressing table, next to your jewellery box. I think of how young and innocent we were back then. I remember being on the church steps grinning like a 11)Cheshire cat, when you said how dashing and handsome I looked in my uniform. The photo is old and faded now, but when I look at it, I only see the bright vibrant colors of our youth. I can still remember every detail of the pretty wedding dress your mother made for you, with its fine delicate lace and pretty pearls. If I concentrate hard enough, I can smell the sweetness of your wedding bouquet as you held it so proudly for everyone to see.

    I remember being so over enjoyed, when a year later, you gently held my hand to your waist and whispered in my ear that we were going to be a family.

    I know both our children love you dearly; they are outside the door now, waiting.

    Do you remember how I panicked like a mad man when Jonathon was born? I can still picture you laughing and smiling at me now, as I 12)clumsily held him for the very first time in my arms. I watched as your laughter faded into tears, as I stared at him and cried my own tears of joy.

    Sarah and Tom arrived this morning with little Tessie. Can you remember how we both hugged each other tightly when we saw our tiny granddaughter for the first time? I can`t believe she will be eight next month. I am trying not to cry, my love, as I tell you how beautiful she looks today in her pretty dress and red shiny shoes, she reminds me so much of you that first day we met. She has her hair cut short now, just like yours was all those years ago. When I met her at the door her smile wrapped around me like a warm glove, just like yours used to do, my darling.world of warcraft power leveling

    I know you are tired, my dear, and I must let you go. But I love you so much it hurts to do so.

    As we grew old together, I would tease you that you had not changed since we first met. But it is true, my darling. I do not see the wrinkles and grey hair that other people see. When I look at you now, I only see your sweet tender lips and youthful sparkling eyes as we sat and had out first picnic next to that small stream, and chased each other around that big old oak tree. I remember wishing those first few days together would last forever. Do you remember how exciting and wonderful those days were?

    I must go now, my darling. Our children are waiting outside. They want to say goodbye to you.

    I wipe the tears away from my eyes and bend my frail old legs down to the floor, so that I can kneel beside you. I lean close to you and take hold of your hand and kiss your tender lips for the very last time.
  • The Man on the Moor

    drove out of Newtown and went to begin my search for the mysterious man on the moor.There were hundreds of the old stone huts on the moor.Barrymore did not know in which of them the mysterious man was living.I had seen the man on the night when Sir Henry and I had chased Selden,so I decided to start my search near that place.

    The path I took ran past Mr Frankland's house,and I saw him standing at his gate He called to me,and invited me to go in and have a drink with him.He had been arguing with the police,and was angry with them. He began to tell me about it.
    wow power leveling,
    'But they will be sorry,'he said.'I could tell them where to look for the escaped prisoner,but I am not going to help them.You see,I have been searching the moors with my telescope,and although I have not actually seen the prisoner,I have seen the person who is taking him food.'

    I thought of Barrymore and Mrs Barrymore's worried faces.But Mr Frankland's next words showed me that I did not need to worry.

    'You will be surprised to hear that a young boy takes food to the prisoner.The boy goes by at about the same time each day,and he is always carrying a bag.Who else can he be going to see?except the prisoner?Come and look through my tele scope,and you will see that I am right.It is about this time each day that the boy goes by.'

    We went up onto the roof,and we did not have to wait long.There was someone moving on a hill in front of the house.I looked through the telescope and saw a small boy with a bag over his shoulder.He looked around to make sure that noboay was following him,and then he disappeared over the hill.wedding dresses,

    'Remember that I don't want the police to know my secret,Dr Watson,'Frankland reminded me.'I'm too angry with them at the moment to help them.'

    I agreed not to tell the police,and said goodbye.I walked along the road while Frankland was watching me,but as soon as I was round the corner,I went towards the hill where we had seen the boy.

    The sun was already going down when I reached the top of the hill.I could not see the boy,and there was nothing else in that lonely place Beneath me on the other side of the hill was a circle of old stone huts.In the middle of the circle was one hut that had a better roof than the others,so it would keep out the wind and the rain.This must be the place where the mysteri ous man was hiding!I would soon know his secret.

    As I walked towards the hut,I saw that someone had certainly been using it.A path had been worn up to the door.I took my revolver out of my pocket,and checked that it was ready to fire.I walked quickly and quietly up to the hut,and looked inside.The place was empty.
    wedding dresses,
    But this was certainly where the man lived.As I looked round the hut,I knew that the mysterious man must have a very strong character.No other person could live in conditions as bad as these.There were some blankets on a flat stone where the man slept.There had been a fire in one corner.There were some cooking pots,and a large bowl half full of wa ter.In the middle of the hut was another large flat stone which was used as a table,and on it was the bag the boy had been carrying.Under the bag I saw a piece of paper with writing on it.Quickly,I picked up the paper and read what was written on it.It said:'Dr Watson has gone to Newtown.'

    I realized that the mysterious man had told someone to watch me,and this was a message from his spy.Was the man a dangerous enemy? Or was he a friend who was watching us to make sure we were safe?I decided I would not leave the hut until I knew.
    Final Fantasy XI GIL,
    Outside,the sun was low in the sky.Everything looked calm and peaceful in the golden evening light.But I did not feel peaceful or calm.I felt frightened as I waited for the mysterious man.

    Then I heard footsteps coming towards the hut.As they came closer,I moved into the darkest corner of the hut.I did not want the man to see me until I had looked closely at him.The footsteps stopped,and I could hear nothing at all.Then the man began to move again,and the footsteps came closer.A shadow fell across the door of the hut.

    'It's a lovely evening,my dear Watson,'said a voice I knew well.'I really think you will enjoy it more out here.'
    aion kinah,
    The Man on the Moor