import java.util.*;
import java.io.*;
import java.text.*;
import java.lang.*;

public class Pascals_Triangle

    public static void main(String[] args) throws IntegerEntryFailException
    {
        new Pascals_Triangle();
    }
    public Pascals_Triangle()
    {
        int lines = 0, index = 0;
        char q = ';';
        String string = null;

        Triangle myTriangle = new Triangle();
        Scanner scan = new Scanner(System.in);

        while(q == ';')
        {
            q = '?';
            System.out.print("How many lines of Pascal's Triangle would you like?  ");
            string = scan.nextLine();
            try
            {
                lines = Integer.parseInt(string);
                if(lines < 1 || lines > 12)
                {
                    throw new IntegerEntryFailException("");
                }
            }
            catch(NumberFormatException exception)
            {
                System.out.println("\nNumber of lines must be a number. Try again.\n");
                q = ';';
            }
            catch(IntegerEntryFailException exception)
            {
                System.out.println("\nMust choose a number between 1 and 12.\n");
                q = ';';
            }
        }
        index = lines;

        System.out.println("\n");
        for(int outer = 0; outer < lines; outer++)
        {
            for(int count = 0; count < index; count++)
                System.out.print(" ");
            for(int inner = 0; inner < outer; inner++)
            {

                System.out.print(myTriangle.Triangle(outer, inner) + " ");
            }
            System.out.println();
            index--;
        }
        System.out.println("\n\n");
    }
    public class IntegerEntryFailException extends Exception
    {
        IntegerEntryFailException(String message)
        {
            super(message);
        }
    }
    public class Triangle
    {
        int one = 0, two = 0;

        public int Triangle(int row, int column)
        {
            if(row == 0 || column == 0 || row == column+1)
                return 1;
            one = Triangle(row-1, column);
            two = Triangle(row-1,column-1);
            return one + two;
        }
    }
}

here is my code.
code runs, I just get incorret values and am having trouble figuring it out
any help would be much appreciated.

Edited 3 Years Ago by Zachary_1

correct values are
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1

values I am getting
1
1 1
1 2 1
1 3 3 1
1 4 5 4 1
1 5 7 7 5 1
1 6 9 10 9 6 1

Recursion does not work that way. At line 79 you are returning fixed values one and two. Recursion is returning the method with new parameters, basically. Not assigning the method to a variable then returning that variable.

So uncomment lines 77 and 78 and change your return statement with methods.

That should work :)

Thank you for your advice. I rewrote my method and got it to work. This is what I used for my method.

public int Triangle(int row, int column)
{
    if (column == 0)
        return 0;
    else if(row == 1 && column == 1)
        return 1;
    else if (column > row)
        return 0;
    else
        return (Triangle(row-1, column-1) + Triangle(row-1, column));

}
This question has already been answered. Start a new discussion instead.