Results 1 to 20 of 23
- 04-07-2011, 04:47 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
for loop problem. any suggestions?
We are learning how to use for loops in class and I'm supposed to come up with 4 triangles, one below the other, made of asterisks. I was able to make the first two (except that I am still figuring out how to make a blank line separating the two). What really has me stumped though is that the next two triangles are supposed to have blank spaces on the left-hand side.
The assignment is to create the triangles with "all asterisks (*) printed by a single statement of the form System.out.print('*'). 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 (each line should begin with the appropriate number of blank spaces."
I'm really new to this, so I'm sorry if there is an obvious direction I should go in that I'm overlooking. Any help would be greatly appreciated!
Here's what the triangles should look like:
**********
*********
********
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
********
*********
**********
My code so far:
- [1]public class TrianglesTest
[2]{
[3] public static void main(String[] args)
[4] {
[5] for(int i=0; i<=10; i++)
[6] {
[7] for(int j=1; j<=i; j++)
[8] {
[9] System.out.print("*");
[10] }
[11] {
[12] System.out.println(" ");
[13] }
[14] }
[15] {
[16] for(int i=10; i>0; i--)
[17] {
[18] for(int j=0; j<i; j++)
[19] {
[20] System.out.print("*");
[21] }
[22] System.out.println(" ");
[23] }
[24] }
[25]
[26] }
[27]}
- 04-07-2011, 04:50 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The stuff you showed of what the result should be only shows 2 of 4 triangles, can you show what the final product will look like? Also, when posting code, use code tags and don't worry about numbering it.
- 04-07-2011, 04:57 AM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
i think /n creates a blank line
- 04-07-2011, 05:01 AM #4
The assignment gives you a hint how to do it. You need to print N spaces and M stars on each line. Take a close look at the example output and you should see a pattern.
You can print a blank line by calling the no arg println method.
- 04-07-2011, 05:16 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Sorry. All 4 triangles should look like this:
and here's my code in code block:*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*
**********
*********
********
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
********
*********
**********
Thanks!Java Code:public class TrianglesTest { public static void main(String[] args) { for(int i=0; i<=10; i++) { for(int j=1; j<=i; j++) { System.out.print("*"); } { System.out.println(" "); } } { for(int i=10; i>0; i--) { for(int j=0; j<i; j++) { System.out.print("*"); } System.out.println(" "); } } } }
- 04-07-2011, 05:22 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Cool! That got my first two triangles separated. Thanks, Ryan10!
Last edited by basla; 04-07-2011 at 05:25 AM. Reason: Oops! That was to Ryan10
- 04-07-2011, 05:27 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What does that code snippet produce?
- 04-07-2011, 05:49 AM #8
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
no problem
- 04-07-2011, 05:57 AM #9
My suggstion of using the no arg println method is better as it is assured to be cross platform compatible.
- 04-07-2011, 06:00 AM #10
- 04-07-2011, 06:03 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
As junky said earlier, whenever you get an assignment similar to this you should be looking for a noticeable pattern. In this case, the way the spaces and stars change in the solution.
- 04-07-2011, 06:28 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
It makes the first 2 triangles.
Also, thanks to Junky for the no arg suggestion. I don't remember going over that one in class so I wasn't sure how to use it and decided to go with the one I was somewhat familiar with. You wouldn't believe how long it takes me to come up with the most basic stuff. It's sooooooo frustrating!
In terms of the pattern, I see that N+M=10, (or 10-M=N or 10-N=M), so as one increments by x the other decrements by x. Is that right? I'm not sure how to write that into code though.
- 04-07-2011, 06:30 AM #13
Sure you do.
Java Code:loop rows { print N spaces print M stars print new line change values }
- 04-07-2011, 06:33 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
For the loops, if you are making 10 lines you will want to do 10 - j stars per line and j spaces per line. Converting it correctly isn't too challenging but it will take some thought on your part.
- 04-07-2011, 03:27 PM #15
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Thanks for all your help! I got one more triangle. Just one more to go now. I tried cutting and pasting the code from my third triangle to use as the basis for my 4th triangle. It compiles ok, but doesn't print the last triangle. Any ideas why? I know what I have for the final triangle isn't correct in terms of what the triangle should look like- I just want to be able to print it so I can do some trial-and-error adjusting based on what I see).
Thanks again for everyone's help!
Here's what it looks like printed:Java Code:public class TrianglePrinting { public static void main(String[] args) { //first triangle {for(int i=1; i<=10; i++)//declare and initialize control variable; set loop continuation condition; increment control variable by 1 (determines number or stars) { for(int j=1; j<=i; j++)//declare and initialize control variable; set loop continuation condition; increment control variable by 1{it will go until number of lines=number of stars) { System.out.print("*");//display * to screen } { System.out.println(" ");//display white space to screen } }} System.out.println("\n");//print new line {//second triangle for(int i=10; i>=0; i--)//declare and initialze control variable; set loop continuation condition; decrement control variable by 1 { for(int j=1; j<=i; j++)//declare and initialze control variable; set loop continuation condition; increment control variable by 1 { System.out.print("*");//display * to screen } System.out.println(" ");//display white space to screen } } System.out.println("\n");//print new line //third triangle int nLines = 10;//initiate nLines for (int i = 1; i <= nLines; i++)//nLines (10) is the max limit. Increment lines starting at 1 and ending at 10. { int nSpaces = i - 1;//initiate nSpaces to one less than the numberof lines int nStars = (nLines - i) + 1;//initiate asterisks to the number of lines for (int j = 1; j <= nSpaces; j++)//declare and initialze control variable; set loop continuation condition; increment control variable by 1 System.out.print(" ");//display white space to screen for (int j = 1; j <= nStars; j++)//declare and initialze control variable; set loop continuation condition; increment control variable by 1 System.out.print("*");//display * to screen System.out.println("");//display white space to screen } System.out.println("\n"); } //fourth triangle int nLines = 10;//initiate nLines { for (int i = 9; i <= nLines; i--)//nLines (10) is the max limit. Increment lines starting at 1 and ending at 10. { int nSpaces = (i - 1) +1;//initiate nSpaces to one less than the numberof lines int nStars = (i - nSpaces);//initiate asterisks to the number of lines for (int j = 1; j <= nSpaces; j++)//declare and initialze control variable; set loop continuation condition; increment control variable by 1 System.out.print(" ");//display white space to screen for (int j = 1; j <= nStars; j++)//declare and initialze control variable; set loop continuation condition; increment control variable by 1 System.out.print("*");//display * to screen System.out.println("");//display white space to screen } } }
*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*
**********
*********
********
*******
******
*****
****
***
**
*
- 04-07-2011, 07:54 PM #16
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
compile this:
Java Code:public class TrianglePrinting { public static void main(String[] args) { for(int i=1; i<=10; i++) { for(int j=1; j<=i; j++) { System.out.print("*");//display * to screen } { System.out.println(" ");//display white space to screen } }//end of for loop System.out.println(" "); for(int i=10; i>=0; i--) { for(int j=1; j<=i; j++) { System.out.print("*");//display * to screen } { System.out.println(" ");//display white space to screen } }//end of for loop System.out.println(" "); int nLines = 10; for(int i=1; i<=nLines; i++) { int nSpaces = i - 1; int nStars = (nLines - i) + 1; for(int j=1; j<=nSpaces; j++) System.out.print(" "); for (int j = 1; j <= nStars; j++) System.out.print("*"); System.out.println(""); }//end of for loop System.out.println(" "); nLines = 10; for(int i=10; i>0; i--) { int nStars = (nLines - i) + 1; int m = 9 - nStars; for(int j=m; j>=0; j--) System.out.print(" "); for (int j = 1; j <= nStars; j++) System.out.print("*"); System.out.println(""); }//end of for loop }//end of main }//end of classLast edited by hamidreza; 04-07-2011 at 07:57 PM.
- 04-08-2011, 02:35 AM #17
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Wow! Thank you so much, Hamidreza! That works beautifully.
Thanks again to everyone who helped me with understanding the problem.
-
Hamidreza did not help you by spoon feeding you the answer. Yes, I know that you will argue that he did, but trust me, our experience here (and we've seen a lot) is that he didn't, that you will learn much more by sweating to the solution yourself.
- 04-08-2011, 02:49 AM #19
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I agree with fubar. Hazim, please don't spoonfeed.
To the op: I know sticking to a problem can get quite frustrating and it's hard to resist when someone supplies you with the code, I urge you to ignore spoonfed answers in the future. When it finally does click, the time you spent learning the solution through hard work will prove to be invaluable.
- 04-08-2011, 03:28 AM #20
Normally I don't post fully coded solutions but I'm sure if anyone hands this in questions will be asked.
Java Code:class Triangles { Triangles(int num) { run(num); } private void print(char c, int rows, boolean left, boolean ascending) { int spaces = (left || ! ascending) ? 0 : rows - 1; int charCount = ascending ? 1 : rows; for(int index = 0; index < rows; index++) { if(! left) { printChars(' ', spaces); } printChars(c, charCount); if(ascending) { charCount++; spaces--; } else { charCount--; spaces++; } System.out.println(); } System.out.println(); } private void printChars(char c, int count) { for(int index = 0; index < count; index++) { System.out.print(c); } } private void run(int num) { print('*', num, true, true); print('*', num, true, false); print('*', num, false, false); print('*', num, false, true); } public static void main(String[] args) { new Triangles(Integer.parseInt(args[0])); } }
Similar Threads
-
JFrame problem - need suggestions
By mystxx in forum Advanced JavaReplies: 2Last Post: 06-19-2010, 10:29 PM -
While loop problem
By mochibon in forum New To JavaReplies: 3Last Post: 04-18-2010, 08:21 PM -
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM -
Some while loop problem need help
By shaggyoo7 in forum New To JavaReplies: 4Last Post: 01-14-2009, 07:16 PM -
Suggestions required for solving a Java problem
By bilal_ali_java in forum Advanced JavaReplies: 3Last Post: 08-16-2008, 01:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks