-1

//college work

Hello, I am trying to print a square by using loops, it also require the user to input the height and width. The Output should look like this

....
.  .
....

any help would be apprciated

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

        Scanner input = new Scanner(System. in );
        System.out.print("Please enter the height of the box: ");
        int x = input.nextInt();

        System.out.println("Please enter a width for the box: ");
        int y = input.nextInt();
        drawbox(x, y);

    }

    static void drawbox(int x, int y) {

        for (int j = 0; j < y; j++) {
            System.out.println("*");

            System.out.println();
            for (int i = 0; i < x - 2; i++) {
                System.out.print("*");
                for (int z = 0; z < y - 2; z++) {
                    System.out.print(" ");
                }
                System.out.println("*");

                for (int i = 0; j < y; j++) {
                    System.out.println("*");
                }

            }
        }
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
user3040990
  • 1
  • 1
  • 1
  • 2
  • 1
    why take two arguments for printing the square? with one argument(user input) u can do it – Vinayak Pahalwan Nov 27 '13 at 09:16
  • In a square: heigth == width. If the user can enter heigth and width you have to print a rectangle. Please clarify what exactly you need to do. – Mailerdaimon Nov 27 '13 at 09:32
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the [edit] link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Apr 25 '18 at 13:19
  • "Any help" ... isn't a valid request here. Please clearly describe the expected and *actual* behavior of your code. Dont leave it to your readers to first digest your requirements, to then figure what exactly your code is doing to *then* figure the problem! – GhostCat Apr 25 '18 at 13:21
  • And unrelated: you are probably talking about a rectangle, not a square. And then: x/y are typically used to name **coordinates**! So call your variables length / height instead! – GhostCat Apr 25 '18 at 13:22

7 Answers7

2

Change your loop from

for(int i = 0; j<y ; j++){
      System.out.println("*");
 }

To

 for(int j = 0; j<y ; j++){
      System.out.println("*");
 }

To draw rectangle, change your drawbox code like:

static void drawbox(int x, int y) {
    for (int i = 0; i < y; i++) {
        System.out.print("*");
    }
    System.out.println();
    for (int i = 0; i < x - 2; i++) {
        System.out.print("*");
        for (int j = 0; j < y - 2; j++) {
            System.out.print(" ");
        }
        System.out.println("*");
    }
    for (int i = 0; i < y; i++) {
        System.out.print("*");
    }
    System.out.println();
}
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

change

  for(int i = 0; j<y ; j++){
      System.out.println("*");
 }

to

  for(int i = 0; i<y ; i++){
      System.out.println("*");
 }
Le Duy Khanh
  • 1,339
  • 3
  • 17
  • 36
0

You could use this(see below). this will print *'s for the first and last row. for the middle rows, it prints a star followed by spaces and closes with a star. It also has another method allStars which reuses code for first and last lines.

static void drawbox(int height, int width){
    for (int i = 1; i <= height; i++) {
        if(i==1 || i==height){
            allStars(width);
        }else{
            System.out.print("* ");
            for(int k = 2; k < width; k++){
                System.out.print("  ");
            }
            System.out.print("*");
            System.out.print("\n");
        }
    }
}
static void allStars(int width){
    for(int k = 1; k <= width; k++){
        System.out.print("* ");
    }
    System.out.print("\n");
}
Sionnach733
  • 4,686
  • 4
  • 36
  • 51
0

Your solution was good enough when issue fixed. But take this as easy way, hope you will get some idea from this

private static void printSquare(int width,int length){
      for(int i=0;i<width;i++){
          StringBuilder stringBuilder=new StringBuilder();
          stringBuilder.append("* ");
          for (int j=2;j<length;j++){
              if(i==0){
                stringBuilder.append("* ");
              }else if(i==width-1){
                stringBuilder.append("* ");
              }else {
                stringBuilder.append("  ");
              }
          }
          stringBuilder.append("* ");
          System.out.println(stringBuilder);
      }
}

When printSquare(5,5);, Out put

* * * * * 
*       * 
*       * 
*       * 
* * * * * 
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

A simple algorithm to draw square by loops

public void draw(int length){
    for(int i = 0; i < length; i++){
        for(int j = 0;j < length; j++){
            if(i!= 0 && j > 0 && i!= length - 1 && j!= length-1){
                System.out.print(" ");
            }
            else System.out.print("*");
        }
        System.out.println();
    }
}
Frank Ngai
  • 16
  • 2
0

Try this one:

import java.util.Scanner;

    public class Rectangle {
        public static void main(String args[]){
             Scanner input = new Scanner(System. in );
                System.out.print("Please enter the height of the box: ");
                int x = input.nextInt();

                System.out.print("Please enter a width for the box: ");
                int y = input.nextInt();
                drawbox(x, y);

            }

            static void drawbox(int x, int y) {

                for (int j = 0; j < y; j++) {
                    System.out.print("* ");
                }
                System.out.println();
                    for (int i = 0; i < x; i++) {
                        System.out.print("* ");
                        for (int z = 0; z < y - 2; z++) {
                            System.out.print("  ");
                        }
                        System.out.println("*");
                        }

                        for (int k = 0; k < y; k++) {
                            System.out.print("* ");
                        }

                }    

        }

Output:

Please enter the height of the box: 10
Please enter a width for the box: 10
* * * * * * * * * * 
*                 *
*                 *
*                 *
*                 *
*                 *
*                 *
*                 *
*                 *
*                 *
*                 *
* * * * * * * * * * 
Jim Cyril
  • 1
  • 1
0

try this

public class HelloWorld{

 public static void main(String []args){
    System.out.println("Hello World");
int row = 15;
int clmn =50;
for(int i= 0;i<row; i++ ){
    for(int j= 0;j<clmn; j++ ){
        if(i == 0 || i == row-1 ||j == 0 || j == clmn){
            System.out.print("*");
        }else{
            System.out.print(" ");
        }

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

}

Suraj Malgave
  • 133
  • 1
  • 4