简体   繁体   English

使用Java for循环绘制形状

[英]Drawing shapes using java for loops

I'm just getting started with Java and my teacher asked me to draw the following two shapes using nested for loops. 我刚开始使用Java,我的老师要求我使用嵌套的for循环绘制以下两种形状。

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

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

However, I was able to draw the first figure using the following code. 但是,我能够使用以下代码绘制第一个图形。

class TriangleDrawing{

    public static void main(String args[]){

        for(int x = 1; x <= 6; x++){

            for(int y = 1; y <= (6-x); y++){

                System.out.print(" ");

            }

            for(int z = 0; z < (x + (x-1)); z++){

                System.out.print("*");

            }

            for(int p = 1; p <= (6-x); p++){

                System.out.print(" ");

            }

            System.out.println();

        }

    }

}

I'm having trouble with drawing the second figure. 我在绘制第二个图形时遇到麻烦。
Can anyone help me out drawing that one, by using nested for loops? 谁能通过使用嵌套的for循环来帮助我画出那个?

It appears to me that the second figure is simply the first figure with the internal asterisks replaced with spaces. 在我看来,第二个数字只是第一个数字,内部的星号用空格代替。

That means you only have to change the z -loop so that, on each line but the first and last, it: 这意味着您只需要更改z -loop,以便在每一行(除了第一行和最后一行)上都可以更改它:

  • prints one asterisk; 打印一个星号;
  • prints N spaces, where N starts at one for the second line, and increases by two for each line you're on; 打印N空格,其中N从第二行的一个开始,到您所在的每一行都增加两个; and
  • print the final asterisk. 打印最后的星号。

For the first and last lines, the code will need to stay the same. 对于第一行和最后一行,代码将需要保持不变。 For the former, you only want one asterisk, for the latter, you want all asterisks. 对于前者,您只想要一个星号,对于后者,您想要所有星号。

And, as an aside, the third loop is totally unnecessary. 而且,顺便说一句,第三个循环是完全不必要的。 There's no point putting spaces at the end of the line (in this case) since no-one can see them. 在行尾没有空格(在这种情况下)是没有意义的,因为没人能看到它们。


Since it's classwork, I urge you to try and implement that yourself. 由于这是课堂作业,我敦促您尝试并自己实现。 For completeness, I include the solution below: 为了完整起见,我提供以下解决方案:

public class Test {
    public static void main(String args[]){
        int sz = 6;

        // First line "*"

        for (int y = 1; y < sz; y++)
            System.out.print(" ");
        System.out.println("*");

        // Middle lines "*   *"

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

        // Final line "*****"

        System.out.print("*");
        for (int y = 1; y < sz * 2 - 2; y++)
            System.out.print("*");
        System.out.println("*");
    }
}

In this block: 在此块中:

for(int z = 0; z < (x + (x-1)); z++){ for(int z = 0; z <(x +(x-1)); z ++){

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

check whether you are in the first and/or last element. 检查您是否在第一个和/或最后一个元素中。 if so, print '*' else, print " " 如果是,则打印“ *”,否则,打印“”

change your for loop which is printing * as: 将打印* for循环更改为:

 for(int z = 0; z < (x + (x-1)); z++){
        if(z==0 || z ==(x + (x-1))-1 || x==6)
                System.out.print("*");
        else
                System.out.print(" ");

    }

instead of 代替

for(int z = 0; z < (x + (x-1)); z++){

            System.out.print("*");

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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