Hi,

I am facing problem with my coding but can't seems to find the problem with it. Can someone guide me?

//for loop
			for (line=1; line <=height; line++)
			{	for(col=1; col <=line; col++)
				{
					if (line%2 !=0)// odd line
					{
						if (col%2 != 0)//odd line,odd col
						System.out.print(A);
					
						else //(col%2 == 0) //odd line, even col
						System.out.print(B);
		
					}
					
					if (line%2 == 0)//even line
					{
						if(col%2 !=0)//even line, odd col
						System.out.print(A);
						
						else //(col%2 == 0)//even line, even col
						System.out.print(B);
					}
				}
			}
		}
		else 
			System.out.println (height + "- Error input!!");
	}

the output should be
AA
BBAA
AABBAA
when height = 3

but instead, my output is AAAABBAABBAA.

Thanks.

Regards,
Heels

//for loop
            for (line=1; line <=height; line++)
            {   for(col=1; col <=line; col++)
                {
                    if (line%2 !=0)// odd line
                    {
                        if (col%2 != 0)//odd line,odd col
                        System.out.print(A);

                        else //(col%2 == 0) //odd line, even col
                        System.out.print(B);

                    }

                    if (line%2 == 0)//even line
                    {
                        if(col%2 !=0)//even line, odd col
                        System.out.print(A);

                        else //(col%2 == 0)//even line, even col
                        System.out.print(B);
                    }
                }
            }
        }
        else 
            System.out.println (height + "- Error input!!");
    }

There's one very obvious problem. You have no println statement (except for the error message) so everything is on one line. Stick one in the loop so you get new lines and see what the pattern is.

Sorry, i've put the println in the innest for loop, but it gives me 6 rows of results instead.

Enter height: 3
AA
BB
AA
AA
BB
AA
AA
BB
AA

I'm getting very very confuse now.

Sorry, i've put the println in the innest for loop, but it gives me 6 rows of results instead.

Enter height: 3
AA
BB
AA
AA
BB
AA
AA
BB
AA

I'm getting very very confuse now.

Well, if putting it in the inner loop gives you too many lines, try putting it in the outer loop instead. Step back and look. How many lines do you want? How many times does it go through the inner loop? How many times does it go through the outer loop? That tells you where to put the println.

Well, if putting it in the inner loop gives you too many lines, try putting it in the outer loop instead. Step back and look. How many lines do you want? How many times does it go through the inner loop? How many times does it go through the outer loop? That tells you where to put the println.

Thanks man! i finally can see it and understand this looping better now. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.