class Eksempler{ public static void main(String[] args){ // Problem: Telle ned fra 3 til og med 1 final int TALL = 3; int tall = TALL; // Tell ned vanlig while (tall != 0){ tall = tellNed(tall); } // Tell ned rekursivt tellNedRekursivt(TALL); // Tell opp rekursivt tellOppRekursivt(1); tellOppRekursivt2(TALL); } public static int tellNed(int tall){ System.out.println("teller ned: " + tall); vent(1000); return tall - 1; } public static void tellNedRekursivt(int tall){ // Basissteg if (tall <= 0){ return; } System.out.println("teller ned rekursivt: " + tall); vent(1000); // rekursjonssteg tellNedRekursivt(tall - 1); } public static void tellOppRekursivt(int tall){ if (tall > 3){ return; } System.out.println("teller opp rekursivt: " + tall); vent(1000); tellOppRekursivt(tall + 1); } public static void tellOppRekursivt2(int tall){ // Basissteg if (tall <= 0){ return; } // rekursjonssteg tellOppRekursivt2(tall - 1); System.out.println("teller opp rekursivt 2: " + tall); vent(1000); } // Hjelpemetode public static void vent(int tid){ try{ Thread.sleep(tid); } catch (InterruptedException e){ System.out.println(e); } } }