[java] Drawing Asterisks via For Loops

Solved

I feel like an idiot...

I'm currently doing an assignment where I need to create a program that creates an "X" and a circle using asterisks, while only using FOR loops with Java. Let me state that I am NOT looking for someone to do my work for me - just a point in the right direction.

Currently as it stands, I am able to get 3/4 of the image displayed for the circle (more of a square, really), however I just can't for the life of me comprehend how to "draw" the final side of the image.

From what I can tell, I need to utilize a FOR loop to insert spaces (System.out.println(" ");) inbetween the left and right sides of my square, however everything I have tried ends up with the image being distorted. More or less, with the code provided below, my "circle" has a top, bottom and left side.

How would I add a right side?


import java.util.Scanner; public static void main(String[] args) {

    //input variables
    Scanner input = new Scanner(System.in);
    String xO;
    String spaces = " ";

    int userSize = 0;

    //asking the questions, setting variables
    System.out.println("Would you like to see an X or O?");
    xO = input.next().toLowerCase();

    System.out.println("What size should it be?");
    userSize = input.nextInt();

    if(xO.equals("x")) {

        for(int row = 0; row < userSize; row++)  {
            spaces += "  ";
            System.out.println(spaces + "*");

        }
    }
    if(xO.equals("o")) {

        for(int w = 1; w < userSize; w++) 

            System.out.print("*");
          

        for(int x = 0; x < userSize; x++) 
            System.out.println("*");

           

        

        for(int z = 0; z < userSize; z++)
            System.out.print("*");

    }

Posted Sep 29, 2016, 10:28 PM UTC

01
[deleted]•

[deleted]

1
RouteableOP•

Solved it myself.

for(int firstRun = 1; firstRun <= userSize; firstRun++) {

            for(int nestedRun = 1; nestedRun <= userSize; nestedRun++) {
                
    
                if(firstRun == 1 || firstRun == userSize || nestedRun == 1 || nestedRun == userSize)
                {
                    System.out.print("*");
                }
                else
                {

                    System.out.print(" ");
                }
            }
            System.out.println(" ");

        }
1