• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

how to make this as an output ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to print this
sas.png
[Thumbnail for sas.png]
 
lowercase baba
Posts: 12751
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:What have you tried?


soory i didnt mentioned my code
want to get above output by modifying this code
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dont think of it as trying to print a triangle, think of it as trying to print out a rectangle partially filled with spaces.



Now look at the number of spaces (marked by '-' ) versus the number of stars on each line. There is a pattern.

Hunter


EDIT: fixed typo
 
fred rosenberger
lowercase baba
Posts: 12751
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to take Hunters comment just a little further, I would look at the number of spaces vs the number of star vs. what line number you are printing.

And, I'd start counting the lines on line 0. So, fill this out:

on line 0, i have ___ spaces and ___ stars
on line 1, i have ___ spaces and ___ stars
etc.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hunter McMillen wrote:dont think of it as trying to print a triangle, think of it as trying to print out a rectangle partially filled with spaces.



Now look at the number of spaces (marked by '-' ) versus the number of stars on each line. There is a pattern.

Hunter


EDIT: fixed typo


but i need to implement this only with the help of " for " loop , no other loops or control structures.
then how to do this , the above programe is typing the stars from right to left
but in this we need to put a 4 empty space in 0 line and then we have to reduce the space , how to achieve this with for loops.
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

naved momin wrote:but i need to implement this only with the help of " for " loop , no other loops or control structures.
then how to do this , the above programe is typing the stars from right to left
but in this we need to put a 4 empty space in 0 line and then we have to reduce the space , how to achieve this with for loops.


You know how to print stars "*", you should also know how to print spaces " ".
You know how to increase number of stars, you should also know how decrease number of spaces.
Follow fred rosenberger advice on "fill in the blanks" before code.
Coding is simply turning algorithm into program, you have to find out the algorithm first.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hunter McMillen wrote:dont think of it as trying to print a triangle, think of it as trying to print out a rectangle partially filled with spaces.



Now look at the number of spaces (marked by '-' ) versus the number of stars on each line. There is a pattern.

Hunter


EDIT: fixed typo


i have made it ,. tricky , but still succeded just with the help of for loop
 
Raymond Tong
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

naved momin wrote:
i have made it ,. tricky , but still succeded just with the help of for loop


Try to remove all the for statements and leave only the print and println statements.
There is a way to make use of for loop to print space x times and print star y times where x + y = 4.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Raymond Tong wrote:

naved momin wrote:
i have made it ,. tricky , but still succeded just with the help of for loop


Try to remove all the for statements and leave only the print and println statements.
There is a way to make use of for loop to print space x times and print star y times where x + y = 4.


i didnt get you actually
as you had said , i have tried , will you throw some solution to this .

it is just printing a star one below the other.
 
Raymond Tong
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

naved momin wrote:i didnt get you actually
as you had said , i have tried , will you throw some solution to this .
it is just printing a star one below the other.


I meant the multiple for loops with label break didn't really make use of the power of for loops.
i.e. You have for loop statement in your code but they all iterate once.

Try to turn the following pseudocode into java code
step 1. iterate (x) from 1 to 4
step 2. print (4-x) spaces
step 3. print (x) stars
step 4. print "line break and/or carriage return"
 
Marshal
Posts: 64705
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Raymond Tong wrote: ... step 4. print "line break and/or carriage return"

You mean "Terminate[s] the current line by writing the line separator string".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!