Anyone know how I could write a simple Java program using loops to get
the following output?
1
1 2
1 2 3
1 2 3 4
I'm usually pretty good with loops but can't seem to get this one.
Thanks!
Chris Smith - 09 Dec 2004 19:41 GMT
> Anyone know how I could write a simple Java program using loops to get
> the following output?
[quoted text clipped - 6 lines]
> I'm usually pretty good with loops but can't seem to get this one.
> Thanks!
It's fairly simple, but you need two nested loops. If you can't figure
it out, please post the code to your attempt. It's hard to help you
unless we can get some idea of what specifically you don't understand
about this task.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Stefan Schulz - 09 Dec 2004 19:53 GMT
> Anyone know how I could write a simple Java program using loops to get
> the following output?
[quoted text clipped - 6 lines]
> I'm usually pretty good with loops but can't seem to get this one.
> Thanks!
1) Create a StringBuffer (or, if using 1.5, a StringBuilder)
2) In each iteration, add the current value of the iterated variable to
the buffer, then a space
3) print the value of the buffer in each iteration
Oh, you wanted actual code?

Signature
Whom the gods wish to destroy they first call promising.
Grant Wagner - 09 Dec 2004 20:08 GMT
> Anyone know how I could write a simple Java program using loops to get
> the following output?
[quoted text clipped - 6 lines]
> I'm usually pretty good with loops but can't seem to get this one.
> Thanks!
Here you go:
public class Lines{static java.io.PrintStream _=System.out;static int
_int(int i){int j;for(j=1;j<i;_.print(j+++" "));return j;}public static
void main(String[]s){for(int i=1;i<=4;_.println(_int(i++)));}}
Anyone who's "pretty good with loops" should have no problem figuring it
out.
I hope your instructor gives me a good mark.
--
Grant Wagner <gwagner@agricoreunited.com