Sitzung: Jeden Freitag in der Vorlesungszeit ab 16 Uhr c. t. im MAR 0.005. In der vorlesungsfreien Zeit unregelmäßig (Jemensch da?). Macht mit!

Javakurs/Übungsaufgaben/Fibonacci/Musterlösung: Unterschied zwischen den Versionen

K (fix alignment)
 
Zeile 1: Zeile 1:
 
<pre>
 
<pre>
 
public class FibonacciNumbers {
 
public class FibonacciNumbers {
 +
public static void main(String args[]) {
 +
printfibonacci(-1, 8);
 +
}
  
  public static void main(String args[]) {
+
/* outputs fibonacci numbers from argument one to argument two */
 +
public static void printfibonacci(int start, int end) {
 +
for (int i = start; i < end; i++) {
 +
System.out.println("fibonacci(" + i + ") = " + fibonacci(i));
 +
}
 +
}
  
    printfibonacci(-1, 8);
+
/* calculates a fibonacci number of n */
  }
+
public static int fibonacci(int n) {
 +
if (0 == n) {
 +
return 0;
 +
}
  
 
+
if (1 == n) {
  /*outputs fibonacci numbers from argument one to argument two*/
+
return 1;
  public static void printfibonacci( int start, int end ) {
+
}
 
 
    for( int i= start; i < end; i++ ) {
 
 
 
      System.out.println( "fibonacci("+ i +") = "+ fibonacci(i) );
 
    }
 
  }
 
 
 
  /* calculates a fibonacci number of n */
 
  public static int fibonacci( int n ) {                             
 
 
 
    if ( 0 == n ) {
 
      return 0;
 
    }
 
 
 
    if ( 1 == n ) {
 
      return 1;
 
    }
 
 
 
    if (n > 1) {
 
      return fibonacci( n-1 ) + fibonacci( n-2 );
 
    } else {
 
      System.out.println ( "Error! Fibonacci undefined for negativ "+
 
                          "values");
 
      return -1;
 
      }
 
  }
 
  
 +
if (n > 1) {
 +
return fibonacci(n - 1) + fibonacci(n - 2);
 +
} else {
 +
System.out.println("Error! Fibonacci undefined for negativ values");
 +
return -1;
 +
}
 +
}
 
}
 
}
 
</pre>
 
</pre>
Zeile 42: Zeile 34:
 
Alternative Lösung
 
Alternative Lösung
 
<pre>
 
<pre>
 
 
 
 
public class Fibonacci {
 
public class Fibonacci {
 
 
 
public static void main(String[] args) {
 
public static void main(String[] args) {
+
System.out.print(fibonacci(5));
System.out.print(fibonacci(5));
 
 
}
 
}
public static int fibonacci(int n){
+
 
if (n<0){
+
public static int fibonacci(int n) {
 +
if (n < 0) {
 
System.out.print("Du hast eine negative Zahl eingegeben:");
 
System.out.print("Du hast eine negative Zahl eingegeben:");
}else{
+
} else {
if(n>1){
+
if (n > 1) {
n = fibonacci(n-1) + fibonacci(n-2);  
+
n = fibonacci(n - 1) + fibonacci(n - 2);
}}
+
}
 +
}
 
return n;
 
return n;
 
}
 
}
 
}
 
}
 
 
</pre>
 
</pre>

Aktuelle Version vom 4. März 2013, 15:47 Uhr

public class FibonacciNumbers {
	public static void main(String args[]) {
		printfibonacci(-1, 8);
	}

	/* outputs fibonacci numbers from argument one to argument two */
	public static void printfibonacci(int start, int end) {
		for (int i = start; i < end; i++) {
			System.out.println("fibonacci(" + i + ") = " + fibonacci(i));
		}
	}

	/* calculates a fibonacci number of n */
	public static int fibonacci(int n) {
		if (0 == n) {
			return 0;
		}

		if (1 == n) {
			return 1;
		}

		if (n > 1) {
			return fibonacci(n - 1) + fibonacci(n - 2);
		} else {
			System.out.println("Error! Fibonacci undefined for negativ values");
			return -1;
		}
	}
}

Alternative Lösung

public class Fibonacci {
	public static void main(String[] args) {
		System.out.print(fibonacci(5));
	}

	public static int fibonacci(int n) {
		if (n < 0) {
			System.out.print("Du hast eine negative Zahl eingegeben:");
		} else {
			if (n > 1) {
				n = fibonacci(n - 1) + fibonacci(n - 2);
			}
		}
		return n;
	}
}