java - Making a numerical Pyramid -


firstly homework i'm not looking answer question pointer go next. i'm supposed take user input (x) in instance 3, , return pyramid looks based on input;

    1   2 1 2 3 2 1 2 3 

i tried break problem down printing 1 x , x down 1 , concatenate two. not working, @ all! code far

import java.util.scanner;  public class q8 {      public static void main(string[] args)      {         scanner user_input = new scanner (system.in);          system.out.println("please enter integer 1-15:");         int input = user_input.nextint();          for(int row=1;row<=input;row++)         {             for(int back=input;back>=2;back--)             {                 system.out.print(back+" ");             }             for(int col=1;col<=row;col++)             {             system.out.print(col+" ");             }              system.out.println();         }         } } 

the problem rather printing above pyramid prints;

 3 2 1  3 2 1 2  3 2 1 2 3 

your problem here

        for(int back=input;back>=2;back--)         {             system.out.print(back+" ");         } 

you looping , printing numbers every time input! means in first row, printing 3 2 1. should initialize back row... although problem in case, pyramid left-aligned.

if want pyramid centered, need fill out space before numbers in rows (except last one). can similar loop, except print empty spaces. pyramid may still come out looking bit weird though, depending on font, because characters not same width; should sufficient though.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -