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