Guest User

Untitled

a guest
Jul 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class PrimitiveRecursive {
  4.     public static void main(String[] args) {
  5.        
  6.         int x = -1, y = -1;
  7.         try {
  8.             x = Integer.parseInt(args[0]);
  9.             y = Integer.parseInt(args[1]);
  10.         } catch (Exception e) {
  11.             System.out.println("Please enter valid numbers. I'll give you another chance.");
  12.         }
  13.         if (x < 0 && y < 0) {
  14.             Scanner scan = new Scanner(System.in);
  15.             System.out.println("Please enter X:");
  16.             try {
  17.                 x = Integer.parseInt(scan.nextLine());
  18.             } catch (Exception e) {
  19.                 System.out.println("you suck!");
  20.                 return;
  21.             }
  22.             System.out.println("Please enter Y:");
  23.             try {
  24.                 y = Integer.parseInt(scan.nextLine());
  25.             } catch (Exception e) {
  26.                 System.out.println("you suck!");
  27.                 return;
  28.             }
  29.         }
  30.         System.out.println("Hang on... computing");
  31.         int result = compute(x, y);
  32.         System.out.println("FINALLY: " + result);
  33.        
  34.     }
  35.  
  36.     private static int compute(int x, int y) {
  37. //      System.out.println("X: " + x + ", Y: " + y);
  38.         if (x == 0) {
  39.             return y+1;
  40.         }
  41.         if (y == 0) {
  42.             return compute(x-1, 1);
  43.         }
  44.         else {
  45.             return compute(x-1, compute(x, y-1));
  46.         }
  47.    
  48.     }
  49.  
  50. }
Add Comment
Please, Sign In to add comment