I would like to write a program which asks the user for a number and prints off the tree, For example 7. Program prints something like that then. If user put 3 tree will start from 3. I think you should get the idea.

"Enter a number: 7"
 
            1
	  2 1 2	
	3 2 1 2 3
      4 3 2 1 2 3 4
    5 4 3 2 1 2 3 4 5  
  6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7

Plan:
1. write loop to print off the correct amounts of colums in the one row like enter: 3 prints off:

3 2 1 2 3

2. write loop to print off the correct amount of rows to make a tree
example:

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

3.Write a loop to make a tree from the step 2.

1
  2 1 2
3 2 1 2 3

So far my code is:

import java.util.Scanner;

public class fourdot17 {
  public static void main(String[] args) {
	
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a number: ");
    int number = input.nextInt();
    int originalNumber=number;
		
    for ( int row = 1; row < originalNumber; row++) {
	System.out.println(" ");
			
			
        while(number>1) {
	  System.out.print(number+" ");
	  number--;
	}
		
	while (number<=originalNumber) {
	  System.out.print(number+" ");
	  number++;
	}
    }
 } 
}

Output from my code looks like that rite now:

7 6 5 4 3 2 1 2 3 4 5 6 7  
8 7 6 5 4 3 2 1 2 3 4 5 6 7  //problem, why does ot start with number 8 ???
8 7 6 5 4 3 2 1 2 3 4 5 6 7  
8 7 6 5 4 3 2 1 2 3 4 5 6 7  
8 7 6 5 4 3 2 1 2 3 4 5 6 7  
8 7 6 5 4 3 2 1 2 3 4 5 6 7

Im stuck on the row number 2. Why it prints me 8 7 6 5 4 3 2 1 2 3 4 5 6 in the 2nd row if i enter number 7 from the keyboard. Should be printed 7 6 5 4 3 2 1 2 3 4 5 6 7 in each row. Any help with the step 3 would be also appreciated.
Thanks

Have you taken a piece of paper and worked out what is to be printed on each line.
The number of spaces and the values of the digits for each row.
Then figure out what variables you need to use to how the values of the digits as they are being printed on each row.

What is the relationship between the row and what is being printed on a row?
Can you describe that relationship in words?

the main problem Ive got rite now is number = 8(number++) in the second row. I tried make an if statemnet like

if ( number > orginalNumber) {
   number= number - 1;

to avoid/delete number 8 in the new row but this is not working. Any advice please..

Once I do it I will be thinking how to realize step 3

ALrite I made it. Second loop while wasnt closed. I added if statement

if ( number++ > originalNumber) {
number=number-2;
}

I added if statement after second loop

My output look like it should be in the step 2. Now can anybody give me any idea how to work with step 3 , please

how to work with step 3

Please explain what your problem is, show the program's output and say what is wrong about the output and show what you want the output to be.

QUESTION FROM THE BOOK
Write a program that prompts the user to enter an integer from 1 to 15 and dispalys piramid,as shown in the following sample run:
OUTPUT:

Enter the number of lines: 7

            1
          2 1 2
        3 2 1 2 3
      4 3 2 1 2 3 4
    5 4 3 2 1 2 3 4 5
  6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7

My code so far:

import java.util.Scanner;

public class fourdot17 {
	public static void main(String[] args) {
	
		Scanner input = new Scanner(System.in);

		System.out.println("Enter a number: ");
		int number = input.nextInt();
		int originalNumber=number;
		
		
		
		for ( int row = 1; row < originalNumber; row++) {
			System.out.println("");
			
			
			//counting down in the one line
			while(number>1 ) { 
			System.out.print(number+" ");
			number--;
			}
			//counting up in the one line
			while (number<=originalNumber ) { 
			System.out.print(number+" ");
			number++;
			}
			//decreases number
			if ( number++ > originalNumber) {
				number=number-2;
			}
		}
	}
}

output from my code:

Enter the number of lines: 7

7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7

I am looking for and advice how to make a piramid from my current code. Do you know what Im talking about rite now ? Thx

You need to think about what is printed on each row/line of the output.
There appears to be some spaces and some digits.
Your code needs to print some spaces and then some digits.
The number of spaces and the number of digits changes for each line.
Write down a description of the number of spaces you are to print for each line before the digits.
How many for the first line?
How many for the second line?
etc for all the lines
Then write a loop that will print the correct number of spaces for each line.
Next work out the number of digits that are to be printed on each line.
How many for the first line? and the second line etc.
Then write a loop to print that number of digits on a line

Next work out the logic so the digits you print on each line are in the correct order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.