Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to make this as an output ?

 
naved momin
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]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
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
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • 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
Bartender
Posts: 12183
34
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.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • 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: 255
2
IntelliJ IDE Java Spring
  • 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: 255
2
IntelliJ IDE Java Spring
  • 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"
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • 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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic