Java square (geometric figure)

Asked

Viewed 373 times

3

I have an exercise that is to form a square with the number we are given.

Here’s the code I made:

int altura=5;
int largura=5;

for(int i=0; i<altura; i++)
{
    for (int j=0; j<largura; j++)
    {
        System.out.print("*");
        System.out.print(" ");
    }

    System.out.print("\n");

} 

The problem is that I would like the square to show only the edges, ie inside would not have the asterisks, or would like it to appear like this:

quadrado

But in my code it appears all "filled".

2 answers

5


That is in your example size 5x5

First see what the positions are

x.y

0.0 0.1 0.2 0.3 0.4
1.0 1.1 1.2 1.3 1.4
2.0 2.1 2.2 2.3 2.4
3.0 3.1 3.2 3.3 3.4
4.0 4.1 4.2 4.3 4.4

So you want me to print the positions

above or x = 0

0.0 0.1 0.2 0.3 0.4

left-hand side or with y = 0

0.0 
1.0 
2.0 
3.0 
4.0 

right-hand side with y = 4 or n-1

0.4
1.4
2.4
3.4
4.4

down side with x = 4 or n-1

4.0 4.1 4.2 4.3 4.4

So:

for(int i=0; i<altura; i++)
{
    for (int j=0; j<largura; j++)
    {

        if ( i==0 || j==0 || i == altura-1 || j == largura-1){
            System.out.print("*");
        } else {
            System.out.print(" ");
        }

        System.out.print(" ");
    }

    System.out.print("\n");

} 
  • Perfect!! I was trying with an if, however I was not getting to the height-1, thank you very much!

4

I would do so:

class Main {
    public static void main(String[] args) {
        int altura = 5;
        int largura = 5;
        for (int j = 0; j < largura; j++) System.out.print("* ");
        System.out.print("\n");
        for (int i = 2; i < altura; i++) {
            System.out.print("* ");
            for (int j = 2; j < largura; j++) System.out.print("  ");
            System.out.print("* \n");
        } 
        for (int j = 0; j < largura; j++) System.out.print("* ");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

You strip what is out of the main loop pattern and avoid needlessly conditionals that makes the code more cyclomatic complexity and less efficient for generating much more processing and breaking the branch Prediction processor.

Then you make the first line in a separate loop (first for), as well as the last one (could even create a function to not repeat the code, but for exercise effect is not necessary) which is the last one for.

Internal lines (for middle) you do considering the asterisk at the beginning (first print() within that for) and at the end of the line (last print() within that for) and what is variable is just the space that stays in the innermost loop that only prints space.

Of course in both row and column you count 2 less because you are doing them out of loop, so I started in 2 and not 0.

  • Thank you very much! It is also a good solution, but I didn’t quite understand why in two is the i and the j are =2

  • As it is in the answer if you are printing two lines out of the way loop out of the pattern you have to print two lines less in the loop (the same goes for the columns), already made out, can not do again these two, so instead of starting with 0 started with 2.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.