easy java question, printing stars
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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() ;
}
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() ;
}
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
This question already asked in Java in General.
Admin please move or remove this post.
Regards,
Sriram.V
For java examples,ebooks,interview questions,visit this blog
http://periodicupdates.blogspot.com/
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
And there is no need for continue; or break; statements.
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
i got it done , thanks a lot
Campbell Ritchie
Marshal
Posts: 81613
593
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
And how did you do it? Please show us.
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
if you know how, do it , (much appreciated)!
i m pretty sure this code can be shorter
Campbell Ritchie
Marshal
Posts: 81613
593
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 13 years ago
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.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Are you sure you don't want to print
or
Look closely there are subtle differences between what you posted
or
Look closely there are subtle differences between what you posted
| Are you here to take over the surface world? Because this tiny ad will stop you! Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |










