nested loop trouble

here's one for all you java gurus:

//import java.util.*;

publicclass RowsofTen

{

//static Scanner console = new Scanner(System.in);

publicstaticvoid main (String[] args)

{

int i, j;

for ( i = 11; i <= 40; i++)

{

for (j = i; j <= i + 9; j++)

System.out.print(j +" ");

System.out.println();

}

}

}

the output is this:

11 12 13 14 15 16 17 18 19 20

12 13 14 15 16 17 18 19 20 21

13 14 15 16 17 18 19 20 21 22

14 15 16 17 18 19 20 21 22 23

15 16 17 18 19 20 21 22 23 24

16 17 18 19 20 21 22 23 24 25

17 18 19 20 21 22 23 24 25 26

18 19 20 21 22 23 24 25 26 27

19 20 21 22 23 24 25 26 27 28

20 21 22 23 24 25 26 27 28 29

21 22 23 24 25 26 27 28 29 30

22 23 24 25 26 27 28 29 30 31

23 24 25 26 27 28 29 30 31 32

24 25 26 27 28 29 30 31 32 33

25 26 27 28 29 30 31 32 33 34

26 27 28 29 30 31 32 33 34 35

27 28 29 30 31 32 33 34 35 36

28 29 30 31 32 33 34 35 36 37

29 30 31 32 33 34 35 36 37 38

30 31 32 33 34 35 36 37 38 39

31 32 33 34 35 36 37 38 39 40

32 33 34 35 36 37 38 39 40 41

33 34 35 36 37 38 39 40 41 42

34 35 36 37 38 39 40 41 42 43

35 36 37 38 39 40 41 42 43 44

36 37 38 39 40 41 42 43 44 45

37 38 39 40 41 42 43 44 45 46

38 39 40 41 42 43 44 45 46 47

39 40 41 42 43 44 45 46 47 48

40 41 42 43 44 45 46 47 48 49

when i need the output to look like:

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

31 32 33 34 35 36 37 38 39 40

[2197 byte] By [nsfoura] at [2007-11-27 5:05:24]
# 1

The iterations of your first loop times the iterations of your second loop should equal the total number of iterations, i.e. 30. And in your code it doesn't =p Another hint... what you want has dimensions 3 x 10. So what do you think the number of iterations should be in each loop to achieve these dimensions?

Anyways, I will not in good conscience implement this method for you without a context. Is it a homework assignment? If not, what is it?

ktm5124a at 2007-7-12 10:23:52 > top of Java-index,Java Essentials,Java Programming...
# 2

first of all, this is a homework problem that i'm having trouble with.

i used your hints and so far i got this:

//import java.util.*;

public class RowsofTen

{

//static Scanner console = new Scanner(System.in);

public static void main (String[] args)

{

int i, j;

for ( i = 1; i <= 3; i++)

{

for (j = i * 10 + 1; j <= 40; j++)

System.out.print(j + " ");

System.out.println();

}

}

}

with the output being:

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

31 32 33 34 35 36 37 38 39 40

i'm so close, i just can't figure out how to stop j from increasing after 20 and after 30

nsfoura at 2007-7-12 10:23:52 > top of Java-index,Java Essentials,Java Programming...
# 3
for ( i = 11; i < 41; i=i+10).....
suparenoa at 2007-7-12 10:23:52 > top of Java-index,Java Essentials,Java Programming...
# 4
use break in your loop
AnanSmritia at 2007-7-12 10:23:52 > top of Java-index,Java Essentials,Java Programming...
# 5
What about using one single loop and insert line break after each round number ?
TimTheEnchantora at 2007-7-12 10:23:52 > top of Java-index,Java Essentials,Java Programming...
# 6

//import java.util.*;

public class RowsofTen

{

//static Scanner console = new Scanner(System.in);

public static void main (String[] args)

{

int i, j;

for ( i = 11; i <= 31; i += 10)

{

for ( j = i; j < i + 10; j+= 1)

System.out.print(j + " ");

System.out.println();

}

}

}

output:

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

31 32 33 34 35 36 37 38 39 40

got it.

nsfoura at 2007-7-12 10:23:52 > top of Java-index,Java Essentials,Java Programming...