Hello,
I am trying to figure out how to return a string from a iterative loop.
I know how to code it using system.out.println, but I need to return the whole value as a string. Below is the code for the println version of the code.
It outputs this:
However I need to return that whole output as a string, not using the println method. This is what I have so far, but it hits the return and breaks before it can continue to loop. (At least that's what I think).
Thanks for the advice/guidance.
I am trying to figure out how to return a string from a iterative loop.
I know how to code it using system.out.println, but I need to return the whole value as a string. Below is the code for the println version of the code.
for (int i = 0; i < 5; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); } System.out.println(); }
It outputs this:
* ** *** **** *****
However I need to return that whole output as a string, not using the println method. This is what I have so far, but it hits the return and breaks before it can continue to loop. (At least that's what I think).
//Size is a integer being passed in, for this example lets say 5 to match above. public static String triangle(int size){ String s = ""; for (int i = 0; i < size; i++) { s = ""; for (int j = 0; j <= i; j++) { s = "*" + "\n"; } } return s; }
Thanks for the advice/guidance.