使用循环的钻石轮廓模式java [英] Diamond outline pattern java using loops

查看:176
本文介绍了使用循环的钻石轮廓模式java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java的新手,为了获得所需的输出,我有点麻烦。
我想在java中使用嵌套循环制作菱形轮廓模式。我需要的输出是这样的:

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
3 3
2 2
1

行数基于用户输入。
到目前为止,我可以想出这个:

pre $ public static void drawDiamond(int n){
int space = n - 1;
for(int i = 1; i <= n; i ++){

for(int j = space; j> = 1; j--)
System .out.print();

(int k = 1; k <= i; k ++)
System.out.print(i +);

System.out.println();
space--;





$ b

这是我输出的n = 5

  1 
2 2
3 3 3
4 4 4 4
5 5 5 5 5

请帮我弄明白这个模式


  1. 忽略顶部和底部底点。那些很容易画出来。我们将创建一个for循环,打印每行包含2个字符的中间部分。
  2. 创建一个方法,该方法将取整数n并打印n个空格。

  3. 找出钻石左右两侧的每条线需要多少空间。这就是:


    左边:

    示例n = 4(与上面相同)。如果n = 4,我们需要打印5行包含2个字符的行。我们需要计算出每条线左侧需要多少空间。 x是行号,y是空格号。





    1 3

    2 2

    3 1

    4 2

    5 3


    现在我们需要一个线性函数y = mx + b,就会创建上面的表。

    答案是y = | x-3 | +1。

    现在,例子n = 4,这个线性函数对于任何n是什么?

    y = | x-(n-1)| +1。

    或者在java中, >
    int y = Math.abs(x - (n-1))+1;

    右边
    现在重复与钻石的右侧。 X是行号,y是所需空间的数量。



    1 2

    2 4

    3 6

    4 4

    5 2


    同样,我们需要一个线性函数来创建这个表格。

    答案是y = 6- | 3-x | * 2。


    再次,对于任何n,你将得到

    int y =(2 *(n-1)) - Math.abs((n-1) - x)* 2;


    填入#s而不是x,并且打印最上方和最下方的点应该很容易,不包括在下面。

    完成所有这些之后,我意识到顶部和底部点不能是一个完美的点(不能做半个空格),所以函数将需要修改以下... ...
    int secondSpace =(2 *(n-1)) - Math.abs(( n-1) - i)* 2;

    将变成
    b $ b int secondSpace =(2 *(n-1)) - Math.abs((n-1) - i)* 2-1;

      public static void drawDiamond(int n){
    int numbLinesWith2xs = 2 * n - 3; (int i = 1; i <= numbLinesWith2xs; i ++){
    int firstSpace = Math.abs(i - (n-1))+1;
    int secondSpace =(2 *(n-1)) - Math.abs((n-1)-i)* 2;

    printSpaces(firstSpace);
    System.out.print(x);
    printSpaces(secondSpace);
    System.out.println(x);



    public static void printSpaces(int n){
    for(int i = 1; i <= n; i ++){
    System.out.print();



    // RESULT for drawDiamond(8):

    xx
    xx
    xx
    xx
    xx
    xx
    xx
    xx
    xx
    xx
    xx
    xx
    xx

    包含所有内容的最终解决方案:

    pre $ public static void drawDiamond(int n) {
    int numbLinesWith2xs = 2 * n - 3;
    printSpaces(n);
    System.out.println(1); (int i = 1; i <= numbLinesWith2xs; i ++){
    int firstSpace = Math.abs(i - (n-1))+1;
    int secondSpace =(2 *(n-1)) - Math.abs((n-1)-i)* 2-1;
    printSpaces(firstSpace);
    System.out.print(secondSpace / 2 + 2);
    printSpaces(secondSpace);
    System.out.println(secondSpace / 2 + 2);
    }
    printSpaces(n);
    System.out.println(1);
    }


    1
    2 2
    3 3
    4 4
    3 3
    2 2
    1


    I'm a novice in java and I'm a little bit of trouble trying to get the output I need. I'm trying to make a Diamond outline pattern using nested loops in java. The output I need is this:

        1
       2 2
      3   3
     4     4
      3   3
       2 2
        1 
    

    the number of rows is based on user input. So far I could come up with this:

    public static void drawDiamond(int n) {
        int space = n - 1;
        for (int i = 1; i <= n; i++) {
    
            for (int j = space; j >= 1; j--)
                System.out.print(" ");
    
            for(int k = 1; k <= i; k++)
                System.out.print(i + " ");
    
            System.out.println();
            space--;
    
        }
    

    and this is my output for n = 5

         1 
        2 2 
       3 3 3 
      4 4 4 4 
     5 5 5 5 5 
    

    Please help me get that pattern

    解决方案

    Alright, this is how you have to look at it.

    1. Ignore the top and bottom points. Those are easy to draw. We will make a for loop that will print the middle section that contains 2 chars per line.
    2. make a method that will take in an integer n and print n spaces.
    3. Figure out how many spaces you need for each line on the left and right sides of the diamond. This is how:

    LEFT SIDE:
    Example n=4 (same as above). We need to print 5 lines that contain 2 chars if n=4. We need to figure out how many spaces each line needs for this left side. x is line # and y is # of spaces.

    x y
    1 3
    2 2
    3 1
    4 2
    5 3

    Now we need a linear function, y=mx+b, that will create the above table.
    The answer is y=|x-3|+1.

    Now, rather than the example of n=4, what will this linear function be for any n?
    y=|x-(n-1)|+1.
    or, in java,
    int y = Math.abs(x - (n-1)) + 1;

    RIGHT SIDE
    Now, repeat with right side of the diamond. X is line # and y is # of spaces required.

    x y
    1 2
    2 4
    3 6
    4 4
    5 2

    Again, we need a linear function that creates this table.
    The answer is y=6-|3-x|*2.

    Again, for any n, you will get
    int y= (2*(n-1)) - Math.abs((n-1) - x)*2;

    Filling in #s rather than x's and printing the top and bottom points should be easy, didn't include it below.
    After doing all this, I realized the top and bottom points cannot be a perfect point (can't do half spaces), so the functions will need modification to the following...
    int secondSpace = (2*(n-1)) - Math.abs((n-1) - i)*2;
    will become
    int secondSpace = (2*(n-1)) - Math.abs((n-1) - i)*2-1;

        public static void drawDiamond(int n) {
            int numbLinesWith2xs = 2*n - 3;
            for (int i = 1; i <= numbLinesWith2xs; i++) {
                int firstSpace = Math.abs(i - (n-1)) + 1;
                int secondSpace = (2*(n-1)) - Math.abs((n-1) - i)*2;
    
                printSpaces(firstSpace);
                System.out.print("x");
                printSpaces(secondSpace);
                System.out.println("x");
            }
        }
    
        public static void printSpaces(int n) {
            for (int i=1; i<=n; i++) {
                System.out.print(" ");
            }
        }
    
     //RESULT for drawDiamond(8):
    
           x  x
          x    x
         x      x
        x        x
       x          x
      x            x
     x              x
      x            x
       x          x
        x        x
         x      x
          x    x
           x  x
    

    final solution with everything included:

    public static void drawDiamond(int n) {
            int numbLinesWith2xs = 2*n - 3;
            printSpaces(n);
            System.out.println("1");
            for (int i = 1; i <= numbLinesWith2xs; i++) {
                int firstSpace = Math.abs(i - (n-1)) + 1;
                int secondSpace = (2*(n-1)) - Math.abs((n-1) - i)*2-1;
                printSpaces(firstSpace);
                System.out.print(secondSpace/2+2);
                printSpaces(secondSpace);
                System.out.println(secondSpace/2+2);
            }
            printSpaces(n);
            System.out.println("1");
        }
    
    
        1
       2 2
      3   3
     4     4
      3   3
       2 2
        1
    

    这篇关于使用循环的钻石轮廓模式java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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