2 Replies - 3048 Views - Last Post: 09 October 2012 - 08:21 AM Rate Topic: -----

#1 RangerDanger816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-October 12

Drawing an open square in java using nested loops

Posted 09 October 2012 - 07:23 AM

public class ForLoops
{
	public static void main(String[] args)
	{
        for(int i = 0; i < 4; i++)
		{
			for(int j = 0; j < 6; j++)
			{
				if(i==0 || i == 4)
				{
					System.out.print("O");
				}
				else if(j == 0 || j == 6)
				{
					System.out.print("O");
				}
				else
				{
					System.out.print(" ");
				}
			}
		}
	}
}


I have to make a square that is 6x4 that looks like this:

OOOOOO
O O
O O
OOOOOO

OOOOOO
OemptyO
OemptyO
OOOOOO (empty) = empty spaces, blanks.

which i is the row(4) and j is the column(6)

and it is just not coming out right. I am also new to Java.

Is This A Good Question/Topic? 0
  • +

Replies To: Drawing an open square in java using nested loops

#2 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Drawing an open square in java using nested loops

Posted 09 October 2012 - 07:33 AM

You have 2 issues to deal with:

  • Your conditions in the for loop will not allow j to become 6 or i to become 4
  • You are not moving to the next line after you print every row


Other than that, you are very close.
Was This Post Helpful? 1
  • +
  • -

#3 RangerDanger816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-October 12

Re: Drawing an open square in java using nested loops

Posted 09 October 2012 - 08:21 AM

public class ForLoops
{
	public static void main(String[] args)
	{
for(int i = 0; i < 4; i++)
		{
			System.out.println();
			for(int j = 0; j < 6; j++)
			{
				if(i==0 || j==0)
				{
					System.out.print("O");
				}
				else if(i==3 || j==5)
				{
					System.out.print("O");
				}								
				else
				{
					System.out.print(" ");
				}
			}
		}//for
	}//main
}//class


oops I accidentally posted it before I could comment on it. But thank you! I figured it out! Above is my new code :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1