绘制用户输入的钻石(不是用户输入的双倍)。

问题描述 投票:0回答:1

所以我一直在尝试用Java绘制一个钻石形状,我已经完成了钻石的上半部分,但下半部分没有按照我的要求打印。它不是减少到最后,而是保持不变,或者是随着它的下降而增加。

enter image description here

import java.util.Scanner;

public class Q1_Diamond{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.print("How many lines? ");
        int lines = sc.nextInt();

        // Top half of Diamond
        for(int i = 0; i < lines/2; i++){
            for(int j = 0; j < (lines-1)-i; j++){
                System.out.print(" ");
            }

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

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

        // Bottom half of Diamond
        // Even number of lines
        if(lines % 2 == 0){
            for(int k = 0; k < (lines/2); k++){

                for(int j = 0; j < (lines/2)+k; j++){
                    System.out.print(" ");
                }

                for(int j = 0; j < (lines/3 - 0.5f); j++){
                    System.out.print("*");
                }

                for(int j = 0; j < lines/2+1; j++){
                    System.out.print("*");
                }
                System.out.println();
            }
        }

        // Odd number of lines
        else{
            not done yet...
        }
    }
}
java draw shapes
1个回答
1
投票

移除 if 条件(即 if(lines % 2 == 0))的下半部分,只需用下面的循环声明重复与上半部分相同的代码即可。

for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) 

完整的代码。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("How many lines? ");
        int lines = sc.nextInt();

        // Top half of Diamond
        for (int i = 0; i < lines / 2; i++) {
            for (int j = 0; j < (lines - 1) - i; j++) {
                System.out.print(" ");
            }

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

            for (int j = 0; j < i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        // Bottom half of Diamond
        for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--)  {
            for (int j = 0; j < (lines - 1) - i; j++) {
                System.out.print(" ");
            }

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

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

一个示例运行:

How many lines? 10
         *
        ***
       *****
      *******
     *********
     *********
      *******
       *****
        ***
         *

另一个样本运行:

How many lines? 11
          *
         ***
        *****
       *******
      *********
     ***********
      *********
       *******
        *****
         ***
          *

[更新]

你可以将常用的代码提取到一个方法中,例如 printLine 如下图所示。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("How many lines? ");
        int lines = sc.nextInt();

        // Top half of Diamond
        for (int i = 0; i < lines / 2; i++) {
            printLine(lines, i);
        }
        // Bottom half of Diamond
        for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
            printLine(lines, i);
        }
    }

    static void printLine(int lines, int i) {
        for (int j = 0; j < (lines - 1) - i; j++) {
            System.out.print(" ");
        }

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

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

0
投票

下面是另一种方法,它的方法略有不同。

  • 由于某些宽度或行数不能精确指定,总是只有一条中间线,这只是提示一个数字。
  • 它计算间距,并在两个循环内调整星号的领域。 第一个做的是上行和中行。 第二个只是做底部部分。
String symb = "*";
Scanner input = new Scanner(System.in);
int max = 0;
while (max <= 1) {
    System.out.print("Enter a positive no > 1: ");
    max = input.nextInt();
}

System.out.println("Width and height are " + (2*max -3)+"\n");
for (int i = 1; i < max; i++) {
    System.out.println(" ".repeat(max - i) + symb);
    symb += "**";
}

for (int i = 2; i < max; i++) {
    System.out
            .println(" ".repeat(i) + symb.substring(2 * i));
}

当提示输入并提供5时,就会打印出来。

Enter a positive no > 1: 5
Width and height are 7

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

0
投票

解决这个问题的另一种方法是确定星号每行中的空格和星号数量的表达式。显然,这需要以行数(n)和我们所在的行来计算。

如果我们将线数从i=0到n-1,我们就可以得到

nAsterisks = 1 + 2 * min(i, n-i-1)

nSpaces = (n+1)/2 - 1 - min(i, n-i-1)

有了这些我们就可以写一些紧凑的Java。

static void printStar(int n)
{
    for(int i=0, k=0; i<n; i++, k=Math.min(i, n-i-1))
    {
        for(int j=0; j<(n+1)/2-1-k; j++) System.out.print(" ");         
        for(int j=0; j<1+2*k; j++) System.out.print("*");           
        System.out.println();
    }
}

对于 printStar(8) 我们得到。

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

printStar(9) 给予

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
© www.soinside.com 2019 - 2024. All rights reserved.