Results 1 to 13 of 13
Thread: pattern using for loop
- 11-21-2012, 04:25 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 7
- Rep Power
- 0
- 11-21-2012, 04:33 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: pattern using for loop
there are definitely more opportunities.
One is to initialize a string with the whole text and use substring.....
- 11-22-2012, 04:26 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 7
- Rep Power
- 0
Re: pattern using for loop
ca yout please type in code for me..
-
Re: pattern using for loop
- 11-22-2012, 04:55 AM #5
Senior Member
- Join Date
- Nov 2012
- Posts
- 105
- Rep Power
- 0
Re: pattern using for loop
You can use a string array: String BLUEJ[] = {B,L,U,E,J}, and then use a backwards for loop: for(int x = 5; x > 0; x--), and print the chars in the array based on x, that way each time one gets tooken off. (P.S., I'm still learning myself, if this has some sort of major flaw, don't yell at me :3)
- 11-22-2012, 07:52 PM #6
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
-
Re: pattern using for loop
Here's another somewhat esoteric solution, but I couldn't figure out how to do mine on one line:
Improvements welcome!Java Code:public static void main(String[] args) { recursivelyPrint("BLUEJ"); } public static void recursivelyPrint(String _) { if (_.isEmpty()) { return; } System.out.println(_); recursivelyPrint(_.replaceAll(".$", "")); }
oops -- no for loop!Last edited by Fubarable; 11-22-2012 at 09:48 PM.
- 11-24-2012, 07:39 AM #8
Member
- Join Date
- Nov 2012
- Posts
- 1
- Rep Power
- 0
Re: pattern using for loop
String str = "BLUEJ";
int len = str.length();
for ( int i = len; i > 0; i--)
System.out.println(str.substring(0,i));
- 11-24-2012, 12:18 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: pattern using for loop
Duh, that's all kid stuff; have a look at this solid solution:
This is the best solution because it acknowledges the trigonometric aspects of the problem; it also implements a serialVersionUID value so it is extremely solid; it has getters and setters so it is extremely object oriented and it doesn't use silly magic numbers and last but not least I had some free time because it's Saturday.Java Code:import java.io.Serializable; public class Bluej implements Serializable { private static final long serialVersionUID= 34598239746L; public static final int LENGTHOFBLUEJ= 4; private static char[] ackermann= { 0, 0, 0, 0, 0 }; private static void set(long cos) { set(LENGTHOFBLUEJ, cos); } private static void set(int sin, long cos) { ackermann[sin]= (char)(cos&255); if (sin != 0) set(sin-1, cos >>> LENGTHOFBLUEJ+LENGTHOFBLUEJ); } private static char get(int tan) { return (char)get(LENGTHOFBLUEJ, tan); } private static int get(int sin, int cos) { return (cos == 0)?(ackermann[sin]^(sin<<LENGTHOFBLUEJ)):(get(sin-1, cos-1)^get(sin, cos-1)); } public static void main(String[] args) { set(serialVersionUID); http://www.java-forums.org/new-java/65374-pattern-using-loop.html for (int acos= 0; acos <= LENGTHOFBLUEJ; acos++) { for (int asin= 0; asin <= LENGTHOFBLUEJ-acos; asin++) System.out.print(get(asin)); System.out.println(); } } }
kindest regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-24-2012, 01:06 PM #10
Member
- Join Date
- Oct 2012
- Location
- ph
- Posts
- 43
- Rep Power
- 0
Re: pattern using for loop
Great men are here...
- 11-24-2012, 01:16 PM #11
Member
- Join Date
- Nov 2012
- Posts
- 7
- Rep Power
- 0
Re: pattern using for loop
hey every1... its been really awsm for me to see so many guyz dere to help....actully its early dayz for me.. and only 10 dayz since i startd learning java.. and u guyz make me feel confident .. thankyou all
:)
- 11-24-2012, 01:17 PM #12
Member
- Join Date
- Nov 2012
- Posts
- 7
- Rep Power
- 0
Re: pattern using for loop
and by the way im learning it from the new boston channel...nyone wanna say something about that???
- 11-24-2012, 04:53 PM #13
Re: pattern using for loop
Please use proper words. This is a programming forum, not SMS chat.
Recommended reading
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
How to prevent infinite loop when using Singleton pattern?
By KSBeyaz in forum New To JavaReplies: 3Last Post: 05-11-2012, 04:22 PM -
strategy pattern and bridge pattern
By jomypgeorge in forum New To JavaReplies: 2Last Post: 12-13-2010, 05:13 AM -
using a loop to form a string pattern
By xquizitpinay in forum New To JavaReplies: 7Last Post: 10-29-2010, 04:06 AM -
Class pattern to generate following pattern:-
By vxs in forum New To JavaReplies: 5Last Post: 07-14-2010, 11:15 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks