Triangle numbers in Java

I'm new to Java. Now I want to learn better loops I did some examples, but I don't know how to make a triangle that looks like this:

111111 
22222
3333
444
55
6

My code so far:

class Pyramid
{
public static void main (String[] args)
{
   int i,n=9,j; 
   for(i=1;i<=n;i++)
   {
       for(j=1;j<=i;j++)  {          
System.out.print(i); }      
System.out.print("\n");        
}}}

But I managed to do it. It looked like this:

1
22
333
4444
55555
666666

How to proceed in reverse order?

Solution

We can use a function int numberforrow (int row) to perform an appropriate conversion The function can then be used like r = numberforrow (I); Print It needs to map this:

row (i) -> display number (r)
6          1
5          2
4          3
3          4
2          5
1          6

I think you can write:)

Look at the relationship between input (I) and output (R) - it may be useful to note that they always add up to the same value, so a little mathematics should be able to do it

(although a function is not strictly required, I find that these functions can help solve the problem, especially in this case, which illustrates the transformation well – it also applies to the case of "higher-level" transformation, such as the original problem; -)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>