This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I make a triangle like this in java?  RSS feed

 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
                    1 2 3 4 5 6 7 8       
                       1 2 3 4 5 6 7               
                          1 2 3 4 5 6              
                             1 2 3 4 5             
                                1 2 3 4      
                                   1 2 3    
                                      1 2     
                                         1  
How do I make a triangle like this in java
Thanks so much     
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would imagine you would write some java code utilising a loop and System.out.print statements.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindsey Brooks wrote:how
Please post your complete code, what so far you have tried although It has an error Or produces wrong output so someone knows the solution may help you.
 
Campbell Ritchie
Marshal
Posts: 55741
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would suggest you start by asking the question in the thread title minus the words, “in Java”. Write down exactly how you would draw that number triangle by hand, which will read something like
space-number-space-number.
That is the first stage. You gradually refine the process to make it more general for each line. Eventually you end up with instructions which are so basic that you can turn them into code. Until you reach that stage, you don't write any code.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you have no specific requirements, this could be done with a single print statement.
 
Jay Schwartzman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend a nested for loop. Outer loop prints the spaces 0-7, inner prints the numbers 8-1. Using a variable let's say x=1, you can increment x each inner loop to get to the next number.
 
Junilu Lacar
Sheriff
Posts: 11151
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Schwartzman wrote:I would recommend a nested for loop. Outer loop prints the spaces 0-7, inner prints the numbers 8-1. Using a variable let's say x=1, you can increment x each inner loop to get to the next number.

That's not right. True, there will be a nested for-loop structure. However, indentation of each line and printing of the numbers for that line are going to be performed in the same iteration. OP already has a pattern she can use from another similar problem. This one is actually simpler because there's only the "indent" and the "print numbers" subtasks involved.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A piece of paper and pencil, drawing like this makes much easier to understand. After knowing what exactly we need to do then better go for code implementation.
Index01234567
012345678
1-1234567
2--123456
3---12345
4----1234
5-----123
6------12
7-------1
 
Campbell Ritchie
Marshal
Posts: 55741
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Schwartzman, welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!