April 21st, 2010, 12:19 AM

[HOMEWORK] Using Loops
Write an application that displays the following patterns
separately, one below the other. Use any loops you like to generate the patterns. All asterisks (*) should be
printed by a single statement of the form
System.out.print( '*' ); which causes the asterisks to
print side by side. A statement of the form
System.out.println(); can be used to move to the next
line. A statement of the form System.out.print( ' ' ); can
be used to display a space for the last two patterns.
There should be no other output statements in the
program. [Hint: The second pattern requires that each
line begin with an appropriate number of blank spaces.]
a.
*
**
***
****
*****
******
*******
********
*********
**********
b.
**********
*********
********
*******
******
*****
****
***
**
*
April 21st, 2010, 12:49 AM

If anybody didn't catch that, OP tried to post
Code:
a.
*
**
***
****
*****
******
*******
********
*********
**********
b.
**********
*********
********
*******
******
*****
****
***
**
*
I'm going to assume you forgot to ask "Any hints as to how I do this?" and that you have every intention of writing the code by yourself.
First part:
There are 10 lines. How do you count from 1 to 10?
Each line has X stars. How do you count from 1 to X?
The second part is exactly the same except there's a third (yes, third) loop. It's a lot like the one used to print the *s.
Write something, give it a shot, and if you have problems post what you wrote and a description of what's wrong.
Comments on this post
April 21st, 2010, 11:17 AM

Whoops, I posted something up at the top originally, i guess i pasted over it when i put in the assignment. Yes, I'm working on it myself and not expecting anyone to do it for me, just looking for pointers. For part A, I'm guessing that I want to recall the previous line somehow, just unsure of how to do it. I'm also completely lost on how to remove an asterisk from each line in B.
Thanks in advance
April 21st, 2010, 12:15 PM

Originally Posted by airegin413
For part A, I'm guessing that I want to recall the previous line somehow, just unsure of how to do it. I'm also completely lost on how to remove an asterisk from each line in B.
You need to think about it differently. There aren't lines of asterisks  they're lines with some writing. That means you print some stuff, a newline, some more stuff, a newline, and so on.
Part A:
You have a bunch of asterisks with a scattering of newlines in between.
The first line has a * then a \n. The second line has ** then a \n.
If you count that way, the Xth line has X asterisks and a newline. That's two loops: one to count lines, one to count (and print) asterisks.
Part B:
You have a mix of spaces and asterisks, also with newlines in between.
The first line has 1 space and 1 asterisk. The second line has 2 spaces and 2 asterisks. The Xth line has X spaces and X asterisks.
It's much like part A except now you need another loop: alongside the one to count lines and to count asterisks, you have one to count spaces.
For both parts that's a total of 5 loops.
Yes, there are better ways of doing it, but that's not what the homework is about. It's trying to teach you how to think about this type of problem.
Question: If you had to describe the output to somebody without pictures, how would you do it?
April 22nd, 2010, 12:14 AM

okay ive been working on this and im getting close; there is just one thing still confusing me. The output is correctly putting the number of spaces at the beginning of the line, but then it is repeating after each star. I've pasted the output code below. Any idea what I'm doing wrong?
for (int i =0; i < 10; i++)
{
for (int j=10; j > i; j)
{
for(int k=0; k<i+1; k++){
System.out.print(" ");
}
System.out.print('*');
}
System.out.print('\n');
}
April 22nd, 2010, 01:12 AM

You nested all three loops. Right now you've told it that it should print a bunch of spaces for each one star rather than a number of spaces followed by a number of stars.
You're very close. Try putting the spaces loop in a different place.
Comments on this post