【问题标题】:Drawing a Diamond with User Input (not double of the user input)使用用户输入绘制菱形(不是用户输入的两倍)
【发布时间】:2020-05-10 15:39:12
【问题描述】:

所以我一直在尝试在 Java 中绘制一个菱形,我已经完成了菱形的顶部,但底部没有按我的意愿打印。它不是在接近尾声时减少,而是保持不变或随着下降而增加。

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】:

    删除下半部分的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();
        }
    }
    

    【讨论】:

    • 感谢您的帮助。我只需要做一个小改动以确保它打印出正确数量的行:)
    • 啊,是的,我本来打算去的,但我不得不等几分钟 :)
    • 您的解决方案适用于偶数行,奇数行您可以从int i = lines / 2 开始绘制下半部分。另外我建议提取打印一行的通用代码以一种新的方法。
    • @MichaelKreutz - 好点。我已将其合并到更新部分。
    • @ArvindKumarAvinash 我想几乎就在那里...起始值 int i = lines / 2; 用于奇数行,int i = lines / 2 - 1;(如您所见)用于偶数行,对吧?
    【解决方案2】:

    解决此问题的另一种方法是确定星号每行中空格和星号数量的表达式。显然,这需要根据行数 (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) 给了

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2014-10-10
      • 2021-11-13
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多