Ask your own question, for FREE!
Computer Science 6 Online
OpenStudy (mokeira):

I want to print out something like this ^^^^^ ^^^^^ ^^^^^ in java. How can I do this . Is there another way other than system.out.print("^^^^^"); i want to do this in as many different ways as possible

OpenStudy (woodrow73):

would System.out.print("^^^^^"); in a for loop that iterates 3 times be what you mean? or are you looking for a different command to print it as?

OpenStudy (mokeira):

is there a way i can use recursion? @woodrow73

OpenStudy (mokeira):

that can work too..can you please show me an example of how i can do that @woodrow73

OpenStudy (e.mccormick):

Recusion is something that calls itself. I do not know why you would do that for a print. As they said, a loop might be good.

OpenStudy (mokeira):

please give me a hint of how I can use the for loop...

OpenStudy (woodrow73):

I have yet to learn recursion- but I'll show you the syntax of a for loop. for(<initialize variable>; <boolean expression>; <update expression>) { }

OpenStudy (mokeira):

can the variable be a string?

OpenStudy (woodrow73):

When the program gets to the for loop- a variable will initialize at the start, like int i = 0; then when it reaches the end of the bracuts, the update expression will take place, like i++, and it will loop until the boolean expression turns false

OpenStudy (mokeira):

ok...i get your point now

OpenStudy (woodrow73):

if you want to print "^^^^^" 3 times, then you put System.out.print("^^^^^"); inside the bracuts, and adjust the for loop header from there

OpenStudy (e.mccormick):

Recursion is a way to a reduce a problem to the simplest form by passing it to itself. Each time it strips off or deals with some of the problem. A loop is a way to repeat the same action 0 or more times. Yes, a loop can do something no times and be bypassed. Seems odd, but this makes sense if you want the loop to only work in some cases, not all.

OpenStudy (mokeira):

say for example I want to print ^ 5 times, will this be right? for(int i =0; i<=5;i++) { system.out.print("^")

OpenStudy (e.mccormick):

No, that would do it 6 times.

OpenStudy (mokeira):

that is interesting @e.mccormick

OpenStudy (mokeira):

should i have put i<=4

OpenStudy (e.mccormick):

You started at 0 and went up to and included 5. 0,1,2,3,4,5 So 6 times. Or just use < 5 because that is 0,1,2,3,4

OpenStudy (mokeira):

ok, that looks better

OpenStudy (woodrow73):

ya, think of it as the i++ will execute right after you print("^"), so you can do what you said mokeira, or what e.mccormick said, or you could even initialize i as i = 1 instead.

OpenStudy (mokeira):

wow, there are so may different ways to think on the same problem

OpenStudy (e.mccormick):

The typical thing in computer science is to be aware of 0. So: `for(int i =0; i<5;i++) system.out.print("^");`

OpenStudy (mokeira):

yeah

OpenStudy (woodrow73):

ya, & if you only have 1 line of code after a for loop, you don't need brackets

OpenStudy (mokeira):

Thank you so much people!!!!! @woodrow73 @e.mccormick Thanks a lot

OpenStudy (e.mccormick):

np. have fun!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!