gareth
gareth

Reputation: 59

Printing a square of stars in Java

Hi I have been given a task for my course and it is to create an algorithm to make a 5 by 5 square like below:

*****
*****
*****
*****
*****

I've spent hours attempting to do it and read tutorials and books. It's so frustrating as I know it must be so easy if you know what you are doing. Can anyone give me any guidance as to where to start?

Upvotes: 0

Views: 23815

Answers (6)

Pete
Pete

Reputation: 11

Here's how I did it:

class Main {
  public static void main(String[] args) {
    int size = 25;
    int pos = 0;
    for(int i = 0; i<size; i++){
      if(pos % 5 == 0){
        System.out.println();
      }
      System.out.print("*");
      pos++;
    }
  }
}

Upvotes: 1

kostja
kostja

Reputation: 61578

If I understood correctly, what you need is a console output with 5 lines of stars. You can outpt text to console with System.out.print() or System.out.println() with the second option making a line break.

As you have to repeat the output, it is advisable to do enclose the output statement in a loop. Better in a nested loop to separate the x and the y axis.

In order to make the output modifiable - for the case you will need to output a 6x6 or 12x15 square tomorrow without any code modification, I would make the limits of the loop parametrized.

All in all, something like this :

public void printStartSquare(int width, int height){
    for(int i = 0; i < height;i++){
        for(int j = 0; j < width;j++){
            System.out.print("*");
        }
        System.out.println("");
    }
}

Upvotes: 0

Fabian Barney
Fabian Barney

Reputation: 14559

If I got you right, then it's about a NxN square with a given N. Your question is just about N := 5, but your comments let me assume that you've to program a more general solution.

Split the work that have to be done into more basic and smaller problems:

  1. create a String that contains * N times.
  2. call System.out.println() with the generated String N times

Upvotes: 2

John B
John B

Reputation: 32969

Seems like you should have a variable x equal to the dimension (5). A for loop i that loops from 1-x. In it a for loop j that loops from 1-x. The j loops outputs *, or appends * to a string. After the j loop, the i loop does a new line.

This solution will allow for a square grid of any size.

int size = input;
for (i=0; i<size; i++){
   for (j=0; j<size; j++){
       // output a single "*" here
   }
   // output a new line here
}

Upvotes: 3

Krystian Cybulski
Krystian Cybulski

Reputation: 11118

This will work for you as well, but the professor will frown that you found the answer online and did not think of it yourself.

System.out.println("*****\n*****\n*****\n*****\n*****");

Upvotes: 1

You probably know and understand how to create a "Hello World" style program in Java. Now think - how would you go about having that same program print 5 times "Hello World"?

After that, think about how you would make it write N times "Hello World".
After that, think about how you would output a series of N stars.

Good luck!

Upvotes: 17

Related Questions