Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

help with java

  • 17-10-2008 5:23pm
    #1
    Registered Users, Registered Users 2 Posts: 788 ✭✭✭


    For this program I need to output Pascals Triangle but print a star ‘x’ for the odd numbers and a space (‘ ‘) for the even numbers instead. I'm not sure how to do this :s Can anyone help?
    public class Pascals {
      
        public static void main(String[] args) {
    
            String[] line = new String[10];
            long[][] rows = new long[10][];
            for (int i = 0; i < rows.length; i++)
            {
                rows[i] = new long[i + 1];
    
                // Outer entries are equal to 1
                rows[i][0] = 1;
                rows[i][rows[i].length - 1] = 1;
    
                // If you're on the second or subsequent row...
                if (i > 0)
                {
                    //Add the bits in the middle
                    for (int j = 1; j < rows[i].length - 1; j++)
                    {
                        rows[i][j] = rows[i - 1][j - 1] + rows[i - 1][j];
                    }
                }
            }
            
            for (int i = 0; i < 10; i++)
            {
                line[i] = "";
                for (int j = 0; j < rows[i].length; j++)   
                {
                    line[i] = line[i] + rows[i][j] + " ";             
                }
            }
            
            // largest line is last line
            int largest = line[9].length();
            // print back the lines
            for(int i = 0; i < 10; i++) {
                // number of white spaces
                int nbSpace = largest - (line[i].length()) / 2;
                for(int j = 0; j < nbSpace; j++)              
                    System.out.print(" ");
                System.out.println(line[i]);
            }
        }
    


Comments

  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    Have you looked into java modulo operator "%" yet?


Advertisement