Used nested loops that display following patterns
* *** ***** *** *
I only able to have this, after that stucked.
for (int i = 0; i < 5; i++) { for (int j = 0; j <= i * 2; j++) { System.out.print("*"); } System.out.println(); }
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Used nested loops that display following patterns
* *** ***** *** *
I only able to have this, after that stucked.
for (int i = 0; i < 5; i++) { for (int j = 0; j <= i * 2; j++) { System.out.print("*"); } System.out.println(); }
Last edited by John Joe; January 17th, 2018 at 11:59 AM. Reason: change QUOTE to CODE
Whatever you are, be a good one
Can you post the program's current output to show what it does?
Describe the pattern of the output:
how many lines are printed?
what goes on each line?
If you don't understand my answer, don't ignore it, ask a question.
Current output is
* *** ***** ******* *********
Last edited by John Joe; January 17th, 2018 at 12:00 PM. Reason: change QUOTE to CODE
Whatever you are, be a good one
Can you describe the pattern of the desired output:
how many lines are printed?
what goes on each line?
How does what is printed on a line relate to what line is being printed?
If you don't understand my answer, don't ignore it, ask a question.
Thanks for your reply Norm, I actually want it to print a diamond.
I have solved it.
for (int i = 1; i < 5; i += 2) { for (int j = 0; j < 4 - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print("*"); System.out.println(); } for (int i = 5; i > 0; i -= 2) { for (int j = 0; j < 4 - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print("*"); System.out.println(); }
Whatever you are, be a good one
You could Wrap the post of the output in code tags to preserve the leading spaces so we can see its shape.want it to print a diamond.
I have solved it.
Glad you solved it.
Note: The code has too many "magic numbers". Can you change it so the number of lines to print is in a variable and then the contents of that variable is used to control the logic?
Then make it a method and execute it with different values.
If you don't understand my answer, don't ignore it, ask a question.
This is the outputYou could Wrap the post of the output in code tags to preserve the leading spaces so we can see its shape.
What did you mean "magic numbers" ?* * * * * * * * *
Whatever you are, be a good one
All the 5s and 4s should be replaced by variables. The 2 is ok because it is used to get half of a value and would never change.
If you don't understand my answer, don't ignore it, ask a question.
Don't understand Did you mean it would be better if I assign a variable to 4 and 5 ?
Whatever you are, be a good one
Replace the numbers 4 and 5 with variables. Don't hard code numbers in the code.
Then write a method that takes the number of rows as its arg.
Call the method with different values for the number of rows.
If you don't understand my answer, don't ignore it, ask a question.
I think you mean this ?
public class Example { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Key in the row you want it to display: "); int k = input.nextInt(); Example x = new Example(); x.getPatternDisplay(k); } public void getPatternDisplay(int k) { for (int i = 1; i < k; i += 2) { for (int j = 0; j < k - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print("*"); System.out.println(); } for (int i = k; i > 0; i -= 2) { for (int j = 0; j < k - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print("*"); System.out.println(); } } }
Whatever you are, be a good one
Yes, something like that. The variable name k is a bit obscure: k??? instead of nbrOfRows that describes what is in it.
If you don't understand my answer, don't ignore it, ask a question.
John Joe (January 18th, 2018)
Thanks, I will aware of the variables used next time
Whatever you are, be a good one