Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- System.out.println("Please enter the name of the method you want to run.");
- String method = scan.next();
- if (method.equals("recursivePrinter")){
- System.out.println("Please enter the input for the method.");
- int num = scan.nextInt();
- recursivePrinter(num);
- }
- if (method.equals("reversePrint")){
- System.out.println("Please enter the input for the method.");
- String word = scan.next();
- reversePrint(word);
- System.out.println();
- }
- if (method.equals("factorial")){
- System.out.println("Please enter the input for the method.");
- int num = scan.nextInt();
- System.out.println(factorial(num));
- }
- }
- public static void recursivePrinter(int levels){
- if (levels > 0){
- System.out.println("Going down! " + levels);
- recursivePrinter(levels-1);
- }
- if (levels != 0) {
- System.out.println("Coming back up! " + levels);
- }
- }
- public static void reversePrint(String str){
- if (str.length() >= 1) {
- System.out.print(str.substring(str.length()-1,str.length()));
- reversePrint(str.substring(0,str.length()-1));
- }
- }
- public static int factorial(int num){
- int result;
- if(num<=1) {
- return 1;
- }
- else {
- result = factorial(num - 1) * num;
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement