Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java triangles  RSS feed

 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, i'm trying to write a small program using nested for-loops that creates a triangle that looks like this

* * * * *
* * * *
* * *
* *
*

so far my code looks like this


Can anyone help me out?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this line... you are counting down right? What should your halting condition be? Hint: j is always <= 5, there is no way it cannot be when you start at 5 and count down.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Got it!
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Another way to do it. Count up, but have a constant to count to. The inner loop can look like this:-Don't write System.out.println(" "); Write System.out.prontln(); only with an i not an o
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!