• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

How do I make a triangle like this in java?

 
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      
 
Bartender
Posts: 1845
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
 
Bartender
Posts: 1248
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • 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.
 
Marshal
Posts: 67444
257
  • 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.
 
lowercase baba
Posts: 12792
51
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.
 
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.
 
Sheriff
Posts: 14756
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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: 1248
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • 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: 67444
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Schwartzman, welcome to the Ranch
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!