Results 1 to 2 of 2
- 05-03-2012, 02:19 AM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Asterisks and looping! surely you have a pritier solution than mine
My task from my book was to output these:
*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*
**********
*********
********
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
********
*********
**********
I did this, with the following code
I just dont like my solutions that much... I feel there are more elegant looping solutions that produce the same output. I was wondering how you professionals would do this (help me think like a programmer). common, I must of cheated with the formatting for the mirrored counterpartsJava Code:package chapter_v; public class TrianglePrintingProgram_5_15 { public static void main(String[] args) { String chain = ""; for(int i = 1 ; i <= 10 ; i++) { chain = chain + "*"; System.out.print(chain); if(chain.length() == i) { System.out.println(); } } System.out.println(); chain = "**********"; for(int i = 10 ; i >= 1 ; i--) { System.out.print(chain); System.out.println(); chain = chain.substring(0, chain.length() - 1); } System.out.println(); chain = "**********"; for(int i = 10 ; i >= 1 ; i--) { System.out.print(String.format("%10s", chain)); System.out.println(); chain = chain.substring(0, chain.length() - 1); } for(int i = 1 ; i <= 10 ; i++) { chain = chain + "*"; System.out.print(String.format("%10s", chain)); if(chain.length() == i) { System.out.println(); } } } }
- 05-03-2012, 02:22 AM #2
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Similar Threads
-
Box of asterisks
By golf_pro90 in forum New To JavaReplies: 6Last Post: 11-01-2011, 10:53 PM -
Someone come resolve this problem of mine, please!!!
By gabriel landry in forum Advanced JavaReplies: 19Last Post: 11-12-2010, 11:32 AM -
Commercials defined by a 1. party application (not mine)
By Masochist in forum New To JavaReplies: 2Last Post: 05-12-2010, 05:48 PM -
one works, mine doesn't
By psychop in forum Java AppletsReplies: 0Last Post: 01-25-2010, 11:36 PM -
Patch Application Stupidity (mine!)
By LoTech in forum New To JavaReplies: 5Last Post: 09-17-2008, 02:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks