Simple code, but cannot understand

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

using System

namespace ConsoleApplication1

public class Class

public static void Main(

for (int i = 1; i < 10; i++

for (int j = 1; j < 10; j++

for (int k = 1; k < 10; k++

Console.WriteLine(i+" "+j+" "+k)








the running result is
6 3
6 3
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 6
6 6
6 6
6 6
6 6
......

but i think the result should be like
1 1
1 1
1 1
.......

Really confuse about it, thank you so much for help!
 
You got 3 loops, and the innermost (k) one will run every time, the midle
one (j) will run each time the innermost one is finished, and the outer
one (i) will run every time the middle one is finished

so the result will be

i, j, k
i, j, k+1
i, j, k+2
....
i, j+1, k
i, j+1, k+1
i, j+1, k+2
....
i, j+2, k
i, j+2, k+1
i, j+2, k+2
....
....
....
....
....
....
i+1, j, k
i+1, j, k+1
i+1, j, k+2
....
i+1, j+1, k
i+1, j+1, k+1
i+1, j+1, k+2
....
i+1, j+2, k
i+1, j+2, k+1
i+1, j+2, k+2
....
....
....
i+2, j, k
i+2, j, k+1
i+2, j, k+2

and so on.

The result will indeed be
1 1 1
1 1 2
and so on, but it will write so fast you won't see the first results
 
Hello, Lionheart!
for (int i = 1; i < 10; i++)
{ ....
6 4 3
6 4 4 ....

but i think the result should be like:
1 1 1
1 1 2
1 1 3
.......

Really confuse about it, thank you so much for help!

The result have more than 900 rows! You must configure your console buffer
to height near 1000 or more :-)
 
Your correct, the output is
1 1
....
9 9

Your problem is your screen buffer size for height is to small. Try setting it to something like 800 to 1000


----- Lionheart wrote: ----

using System

namespace ConsoleApplication1

public class Class

public static void Main(

for (int i = 1; i < 10; i++

for (int j = 1; j < 10; j++

for (int k = 1; k < 10; k++

Console.WriteLine(i+" "+j+" "+k)








the running result is
6 3
6 3
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 6
6 6
6 6
6 6
6 6
.....

but i think the result should be like
1 1
1 1
1 1
......

Really confuse about it, thank you so much for help!
 
Back
Top