Wednesday, 14 March 2007

Output 5 results per line in Java?


Well, I have my fantastic new Java book but can I work out how to output 5 results per line? Nada!





I know! use count but how?

All I get so far is each result repeated 5 times?

1,1,1,1,1, 2,2,2,2,2, 3,3,3,3,3, etc etc?

I need it to do this:

1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
etc etc

The code I have tried is something like this:

for(i = startvalue, i <= endvalue, i++){

int count = 0;
while(count <= 5)
Screen.out.print(values + ", ");
count++;
}

Am I missing "\n" somewhere or something like that?

2 comments:

  1. put a { after your while loop, and after count++;} that way count++ will be executed inside the loop, and will actually give you what you want.

    you should also print a \n after the while loop so that you will have new lines.

    ReplyDelete
  2. Thank you for that incredibly prompt reply! Glad to know I was close anyway! Thanks again!

    ReplyDelete