Hallo,
ich bin mit folgendem Problem konfrontiert.
Ich soll ein Programm erstellen, welches die Anzahl der Stufen einer Treppe einliest und die Anzahl der Möglichkeiten diese Treppe zu "überwinden" zurückgibt. Dabei darf der Typ, der die Treppe überwinden möchte entweder eine Stufe nehmen oder zwei Stufen.
Soweit so gut, also Rekursion (Fibonacci), kein Ding ...
Jetzt soll ich aber in Teilaufgabe 2 die Anzahl, wie viele Treppen überwunden werden sollen, einlesen und mit dieser das ganze berechnen.
Also für max. 3 Stufen pro Schritt soll da stehen:
return possibilities(height - 1) + possibilities(height - 2) + possibilities(height - 3);
und für max. 4 Stufen pro Schritt soll da stehen:
return possibilities(height - 1) + possibilities(height - 2) + possibilities(height - 3) + possibilities(height - 4);
....
Aber wie geht das? (mit nur 1 Methode)
Ich kann ja schlecht 30 Methoden schreiben um die Fälle bis 30 Stufen pro Schritt abzudecken :/
ich bin mit folgendem Problem konfrontiert.
Ich soll ein Programm erstellen, welches die Anzahl der Stufen einer Treppe einliest und die Anzahl der Möglichkeiten diese Treppe zu "überwinden" zurückgibt. Dabei darf der Typ, der die Treppe überwinden möchte entweder eine Stufe nehmen oder zwei Stufen.
Soweit so gut, also Rekursion (Fibonacci), kein Ding ...
Java:
import java.util.Scanner;
public class Treppen {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Geben Sie die Hoehe der Treppe ein");
int height = inputHeight();
boolean checker = checker(height);
if (checker == true) {
int possibilities = possibilities(height);
System.out.println("Anzahl der Moeglichkeiten: " + possibilities);
} else {
System.err.println("Fehler, die Hoehe der Treppe muss eine natuerliche Zahl groesser 0 sein!");
} // end of if command check if height is ok input
} // end of method main
/**
* input an int (steps of a staircase), save it in int height
*
* @return int height, steps of a staircase
*/
private static int inputHeight() {
Scanner scanner = new Scanner(System.in);
int height = scanner.nextInt();
scanner.close();
return height;
} // end of method inputHeight
/**
* checks if int height is greater 0 or not
*
* @param int height, steps of a staircase
* @return true, if int height > 0 | false, if int height is not > 0
*/
private static boolean checker(int height) {
if (height > 0) {
return true;
} else {
return false;
} // end of if command check if height is ok
} // end of method checker
/**
* calculates the possibilities of completing the staircase
*
* recursive call of possibilities with (int height - 1) +
* recursive call of possibilities with (int height - 2)
* until height is 1 -> return 1
* or height is 2 -> return 2
*
* @param int height, steps of a staircase (left)
* @return int, possibilities of completing the staircase
*/
private static int possibilities(int height) {
if(height == 1) {
return 1;
}
if(height == 2) {
return 2;
}
return possibilities(height - 1) + possibilities(height - 2);
} // end of method possibilities
} // end of class treppen
Jetzt soll ich aber in Teilaufgabe 2 die Anzahl, wie viele Treppen überwunden werden sollen, einlesen und mit dieser das ganze berechnen.
Also für max. 3 Stufen pro Schritt soll da stehen:
return possibilities(height - 1) + possibilities(height - 2) + possibilities(height - 3);
und für max. 4 Stufen pro Schritt soll da stehen:
return possibilities(height - 1) + possibilities(height - 2) + possibilities(height - 3) + possibilities(height - 4);
....
Aber wie geht das? (mit nur 1 Methode)
Ich kann ja schlecht 30 Methoden schreiben um die Fälle bis 30 Stufen pro Schritt abzudecken :/