简体   繁体   中英

Printing a rectangle - I want an additional print but I can't get it work

Made an own task for me to learn programming in java, about printing in particular.

As example, we have input 8 . Then we must get as output a rectangle made of "+", so it would look like this:

++++++++
+      +
+      +
+      +
+      +
+      +
+      +
+      +
++++++++

The following code will do this (if you want me to explain it, please tell me and I will do it):

public class Printest{
    public static void main(String[] args){

        int x = Integer.parseInt(args[0]);
        for(int i=0; i<x; i++){
            System.out.print("+");
        }
        System.out.println("");
        for(int i=0; i<x-1; i++){
            System.out.print("+");
            for(int j=0; j<x-2; j++){
                System.out.print(" ");
            }
            System.out.print("+");
            System.out.println("");
        }
        for(int j=0; j<x; j++){
            System.out.print("+");
        }
    }
}

But now I want add an additional feature so there is a cross inside of this square, how can I do it? I have tried to add some if-statements but I always ended up shifting the rectangles left and right edges.. : /

I want it looks like this to be more precise:

+++++++++
+*     *+
+ *   * +
+  * *  +
+   *   +
+  * *  +
+ *   * +
+*     *+
+++++++++

How would you do it? if-statements doesn't seem to work and I have the feeling it's not possible to implement this in my code because of the multiple, separate for-loops..?

By the way, is there maybe a much easier way to get this work (just print the rectangle)? I think I took a very complicated and unefficient way. I would be very interested in knowing easier ways, if they aren't more complicated :P

By the way, is there maybe a much easier way to get this work (just print the rectangle)? I think I took a very complicated and unefficient way.

Actually yes! You can do it with 2 for loops:

public class RectangleWithShapesExample {
    public static void main(String[] args) {
        int number = 8;

        for (int i = 0; i < number; i++) {
            for (int j = 0; j < number; j++) {
                if (i == 0 || i == number - 1) {
                    System.out.print("+");
                } else {
                    if (j == 0 || j == number - 1) {
                        System.out.print("+");
                    } else {
                        System.out.print(" ");
                    }
                }
            }
            System.out.println("");
        }
    }
}

That alone, prints the rectangle of size 8, then, if you add this else-if statement:

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

The first condition above tells your program to print a * diagonal like this: \\ (that orientation), and the second one, evaluates it to print it in the other direction ( / )

You can print the * cross, so, in the end your whole program looks like this:

public class RectangleWithShapesExample {
    public static void main(String[] args) {
        int number = 9;

        for (int i = 0; i < number; i++) {
            for (int j = 0; j < number; j++) {
                if (i == 0 || i == number - 1) {
                    System.out.print("+");
                } else {
                    if (j == 0 || j == number - 1) {
                        System.out.print("+");
                    } else if (i == j || i == (number - j - 1)) {
                        System.out.print("*");
                    } else {
                        System.out.print(" ");
                    }
                }
            }
            System.out.println("");
        }
    }
}

And it's output is:

++++++++
+*    *+
+ *  * +
+  **  +
+  **  +
+ *  * +
+*    *+
++++++++

And with 9 elements:

+++++++++
+*     *+
+ *   * +
+  * *  +
+   *   +
+  * *  +
+ *   * +
+*     *+
+++++++++

As a suggestion, I recommend you to space a little your operation signs from your variables to make your code a little more readable and make your variables more descriptive ( x to number for example)

You need to find relationships between the corners of the cross/star. Take this star for example of size 5.

  0 1 2 3 4
0 *       *
1   *   *
2     *
3   *   *
4 *       *

In a cross in the diagonal from (0,0) to (4,4), indices are the same (in the code this means row == col ).

Also, you can notice that in the diagonal from (0,4) to (4,0) indices always sum up to 4, which is size - 1 (in the code this is row + col == size - 1 ).

Therefore in the code, you will need to loop through rows and then through columns. Each time you have to check if the above conditions are fufilled.

For the + border simply check when either row or col is 0 , and also when equal to size - 1 .

Lastly you print the border first so the corners of the cross are not * but rather + .

Code:

class Main {
  public static void main(String[] args) {
    printCross(8); //Vertical size of rectangle/cross
  }

  public static void printCross(int size) {
    for (int row = 0; row < size; row++) {
      for (int col = 0; col < size; col++) {
        if (row == 0 || col == 0 || row == size - 1 || col == size - 1)
          System.out.print('+');
        else if (row == col || row + col == size - 1)
          System.out.print('*');
        else
          System.out.print(" ");
      }
      System.out.println();
    }
  }
}

Try it here!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM