Since the task looks really difficult in reality, it is easy. It may seem difficult at first. Because you are not thinking about the end result, not the route that leads to the result.
To change this, we can use the old coding rule, divide and conquer . This exam teaches us to find similarities in a complex problem that can reduce the underlying problem to a simple task that we are able to perform. In other words, we split our big problem into a few painters that can be solved very easily, and in the end we combine small results with big ones.
So, to start with your problem.
Q1: How to print pytamind numbers?
As we do not know this, let's focus on something else.
To improve our observation, we can add some bacground details.
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 0 _ _ _ _ _ _ _ _ 9 _ _ _ _ _ _ _ _ 1 _ _ _ _ _ _ _ 8 9 8 _ _ _ _ _ _ _ 2 _ _ _ _ _ _ 7 8 9 8 7 _ _ _ _ _ _ 3 _ _ _ _ _ 6 7 8 9 8 7 6 _ _ _ _ _ 4 _ _ _ _ 5 6 7 8 9 8 7 6 5 _ _ _ _ 5 _ _ _ 4 5 6 7 8 9 8 7 6 5 4 _ _ _ 6 _ _ 3 4 5 6 7 8 9 8 7 6 5 4 3 _ _ 7 _ 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 _ 8 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
Now is the time to watch.
From this observation, we can come up with the following ideas.
Idea: a pyramid is a construction of two triangles.
Conclusion: it is easier to write half of this pyramid. So let's rephrase the acidic problem.
Q2: How to write a serial number that looks like a triangle?
It is really simple, we just need two loops in order for the first loop to be responsible for columns, others for rows.
for(int column = 1; column <= 9; column++) { for(int row = 1; row <= 9; row++) { if(column ## row) { // Observe what will happen if we use == or <= or > or <> System.out.print(row); } else { System.out.print(" "); } } System.out.println(' ');
}
When you complete your first task, you will be able to print coils, triagles, strings of numbers on screean.
So, when we know how to print a triangle as follows:
r c 1 2 3 4 5 6 7 8 9 1 _ _ _ _ _ _ _ _ 9 2 _ _ _ _ _ _ _ 8 9 3 _ _ _ _ _ _ 7 8 9 4 _ _ _ _ _ 6 7 8 9 5 _ _ _ _ 5 6 7 8 9 6 _ _ _ 4 5 6 7 8 9 7 _ _ 3 4 5 6 7 8 9 8 _ 2 3 4 5 6 7 8 9 9 1 2 3 4 5 6 7 8 9
We need to change your code that is more suitable, usually operations in the computer world begin from scratch, and not from one.
r c 0 1 2 3 4 5 6 7 8 0 _ _ _ _ _ _ _ _ 9 1 _ _ _ _ _ _ _ 8 9 2 _ _ _ _ _ _ 7 8 9 3 _ _ _ _ _ 6 7 8 9 4 _ _ _ _ 5 6 7 8 9 5 _ _ _ 4 5 6 7 8 9 6 _ _ 3 4 5 6 7 8 9 7 _ 2 3 4 5 6 7 8 9 8 1 2 3 4 5 6 7 8 9
When you have time with this, stop and think.
Why should we repeat all these operations for each row? If we could put some values that we do not need to think and calculate, they are more focused on writing the entire result on the screen.
The solution to this problem is arrays and the apporach concept, known as dynamic programming . In this approach, we are trying to remember some things that will be used for futre operations.
So, like a worm, just assign a number and an array instead of printing them.
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [9] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [8] [9] [ ] [ ] [ ] [ ] [ ] [ ] [7] [8] [9] [ ] [ ] [ ] [ ] [ ] [6] [7] [8] [9] [ ] [ ] [ ] [ ] [5] [6] [7] [8] [9] [ ] [ ] [ ] [4] [5] [6] [7] [8] [9] [ ] [ ] [3] [4] [5] [6] [7] [8] [9] [ ] [2] [3] [4] [5] [6] [7] [8] [9] [1] [2] [3] [4] [5] [6] [7] [8] [9]
In this case you should come up with a code
int[] array = new int[9]; for(int column = array.length; column > 0 ; column--) { for(int row = 0; row <= array.length; row++) { if(column == row) { array[row-1] = column; } } System.out.println(Arrays.toString(array)); }
So, what is clear from this code is that for each step we use only one value. It is presented below
9 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [9] -Step one we put nine 8 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [8] [ ] -Step two we put eight 7 [ ] [ ] [ ] [ ] [ ] [ ] [7] [ ] [ ] 6 [ ] [ ] [ ] [ ] [ ] [6] [ ] [ ] [ ] 5 [ ] [ ] [ ] [ ] [5] [ ] [ ] [ ] [ ] 4 [ ] [ ] [ ] [4] [ ] [ ] [ ] [ ] [ ] 3 [ ] [ ] [3] [ ] [ ] [ ] [ ] [ ] [ ] 2 [ ] [2] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 1 [1] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
After nine steps, we fill the entire array with numbers.
We still do not see the result on the screen. To do this, we must print a whole array at every step. First we need to print it from left to right and after that to the end.
And the code that does the magic should look like this:
public static void pyramide(int levels) { int[] tab = new int[levels]; for(int row = tab.length; row > 0; row--) { tab[row-1] = row; //Print left for(int i=0; i < tab.length; i++) { if(tab[i] != 0) { System.out.print(tab[i]); } else { System.out.print(' '); } } //Print right for(int i= tab.length-2; i >= row - 1; i--) { if(tab[i] != 0) { System.out.print(tab[i]); } } System.out.println(""); } }