打印矩形-我需要其他打印,但无法正常工作 [英] Printing a rectangle - I want an additional print but I can't get it work

查看:93
本文介绍了打印矩形-我需要其他打印,但无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己做了一个任务,以学习Java编程,特别是关于打印.

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

例如,我们输入了8.然后,我们必须获得由"+"制成的矩形作为输出,因此看起来像这样:

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("+");
        }
    }
}

但是现在我想添加一个附加功能,以便在该正方形的内部有一个十字形,我该怎么做?我试图添加一些if语句,但是我总是最终将矩形移至左右边缘..:/

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:

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

您将如何做? if语句似乎不起作用,并且我觉得由于多个单独的for循环,无法在我的代码中实现它..

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..?

顺便说一句,也许有更简单的方法来完成这项工作(只需打印矩形)?我认为我采取了一种非常复杂且效率低下的方式.如果它们不是更复杂的话,我会很想知道更简单的方法: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. 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.

实际上是!您可以使用2个for循环来完成此操作:

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("");
        }
    }
}

仅此一项,就打印大小为8的矩形,然后,如果您添加以下else-if语句:

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("");
        }
    }
}

其输出为:

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

有9个元素:

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

作为建议,我建议您在变量中留一些操作符,以使代码更具可读性,并使变量更具描述性(例如,从xnumber)

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)

这篇关于打印矩形-我需要其他打印,但无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆