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
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?
is there a way i can use recursion? @woodrow73
that can work too..can you please show me an example of how i can do that @woodrow73
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.
please give me a hint of how I can use the for loop...
I have yet to learn recursion- but I'll show you the syntax of a for loop. for(<initialize variable>; <boolean expression>; <update expression>) { }
can the variable be a string?
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
ok...i get your point now
if you want to print "^^^^^" 3 times, then you put System.out.print("^^^^^"); inside the bracuts, and adjust the for loop header from there
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.
say for example I want to print ^ 5 times, will this be right? for(int i =0; i<=5;i++) { system.out.print("^")
No, that would do it 6 times.
that is interesting @e.mccormick
should i have put i<=4
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
ok, that looks better
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.
wow, there are so may different ways to think on the same problem
The typical thing in computer science is to be aware of 0. So: `for(int i =0; i<5;i++) system.out.print("^");`
yeah
ya, & if you only have 1 line of code after a for loop, you don't need brackets
Thank you so much people!!!!! @woodrow73 @e.mccormick Thanks a lot
np. have fun!
Join our real-time social learning platform and learn together with your friends!