• Post Reply Bookmark Topic Watch Topic
  • New Topic

easy java question, printing stars  RSS feed

 
Greenhorn
Posts: 23
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to print this figure




how do i do this? using for loops ?
i tried this:

for(int i = 0 ; i < 10 ; i++){
for(int j = 0 ; j < 10 ; j++){
if( i < j ) System.out.print("*) ;
else continue ;
}
System.out.println() ;
}
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont ask same question in multiple categories.
This question already asked in Java in General.

Admin please move or remove this post.

Regards,
Sriram.V
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks as though you are guessing about your code. You ought to write it on paper before you try writing any code. Turn your computer off and write down the algorithm. When you have got that written, you will find it easy to write the code.
And there is no need for continue; or break; statements.
 
kambar bek
Greenhorn
Posts: 23
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it done , thanks a lot

 
Campbell Ritchie
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how did you do it? Please show us.
 
kambar bek
Greenhorn
Posts: 23
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
kambar bek
Greenhorn
Posts: 23
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i believe someone can refactor it , and write shorter code


if you know how, do it , (much appreciated)!


i m pretty sure this code can be shorter
 
Campbell Ritchie
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is quite a lot you can refactor.
Names of identifiers should not normally use _ characters.
you should not use == false or == true. If you have a boolean b, write if (b) ... or if (!b) ...
It looks confused with the nested loops. I would suggest you create a printLineOfCharsWithSpaces(int lineLength, int spaces, char c) method, so if it is called like this printLineOfChars(6, 3, '+'), it prints “   + + + + + + [line-end]”, that is 3 spaces and 6 +s separated by single spaces. You can obviously change that depending on what you intend to print.
I would suggest you find out about the Math#abs() method, so you can count down from 3 to 0 and back up to 3 (that makes 7 lines).
If you put the right conditions on the loop, you can avoid break; and continue; statements.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
You should not use == false or == true. If you have a boolean b, write if (b) ... or if (!b) ...


And once you fix that, rather than


You can just skip the second else and write



And for ease of readability, you may want to change the order, so it becomes


although that depends on the specific conditions and semantics of what you're testing.
 
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you don't want to print



or




Look closely there are subtle differences between what you posted
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!