Results 21 to 23 of 23
- 04-08-2011, 03:57 AM #21
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
I actually did figure it out on my own before his response, but I really appreciate his help. It was good to look and see the solution he came up with vs what I did and realize there are many ways to solve it.
I do actually like it though that you all don't typically spoon feed answers. I know I learn better when I have to figure it out. Some guidance is much appreciated though.
In short, thanks again to all!
Here's what I came up with for my code. It has a ton of documentation notes to myself explaining what I'm doing, so sorry if that makes it hard to read:
Java Code:public class TrianglePrinting //name of program { //Main method begins execution of Java program public static void main(String[] args) { //first triangle for(int i=1; i<=10; i++)/*declare and initialze control variable; set loop continuation condition; increment 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 }//end for loop 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 } //end for loop System.out.println("\n");//print new line //third triangle int nLines = 10;//declare and initialize nLines for (int i = 1; i <= nLines; i++)/*declare and initialize control variable; set loop continuation condition; Increment control variable by 1.*/ { 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 }//end for loop System.out.println(" \n");//print new line //fourth triangle for (int i = 1; i <= nLines; i++)/*declare and initialize control variable; set loop continuation condition; Increment control variable by 1.*/ { int nSpaces = (nLines - i) + 1;/*declare and initialize nSpaces to one less than the numberof lines*/ int nStars = 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 }//end for loop }//End main method }//End class----jGRASP exec: java TrianglePrinting
*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*
**********
*********
********
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
********
*********
- 04-08-2011, 05:00 AM #22
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I am glad you solved it. There will always be more than one way to solve anything. If you wanted you could probably do this in much more complicated ways. The more you work with it, the easier the logic will become. If you get really stuck, come here and ask for help as a last resort. This is how I have been learning java. TBH, I didn't even know how to declare arrays in the beginning of January.
If you are satisfied with the help you have received here, please mark the thread solved with the thread tools at the top.
- 04-08-2011, 06:28 PM #23
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 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