首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用java打印diamonds+alphabet图形

用java打印diamonds+alphabet图形
EN

Stack Overflow用户
提问于 2019-03-30 14:56:53
回答 1查看 186关注 0票数 2

问题是:给定两个数字H和N,请使用*绘制N颗高H的钻石。另外,对于每一颗N颗钻石,也请使用o绘制另一颗较小的钻石,其高度为H2,与先前的钻石中心相同。每个外部钻石都有三个连续的英文字母,从A开始依次递增,如下所示。

但我只知道字母,结果应该是

代码语言:javascript
复制
      *           *
    *   *       *   *
  *   O   *ABC *  O  *DEF
    *   *       *   *
      *           *

我不知道怎么在*后面加上字母表,而不把钻石弄糟。

代码语言:javascript
复制
   import java.util.Scanner;
    public class manydiamond {
        public static void main(String[] args) {
            Scanner scn=new Scanner(System.in);
            System.out.println("Enter the height:");
            int h=scn.nextInt();
            System.out.print("How many diamonds?:");
            int a=scn.nextInt();
            char[] letter={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
            //top of diamond
            for(int n=1;n<=h;n++) {         //which row
            for(int j=1;j<=a;j++)               //how many diamond
                {
                for(int k=1;k<=h-n;k++)         //left space
                {
                    System.out.print(" ");
                }
                for(int m=1;m<=2*n-1;m++)       //star
                {
                    if((m==1||m==2*n-1))
                    System.out.print("*");  
                    else if((n>=3)&&(m==3)||(m==2*n-3))
                    System.out.print("O");
                    else
                    System.out.print(" ");
                }
                for(int space1=1;space1<=-n+h;space1++)     //right space
                {   
                    System.out.print(" ");
                }   
                if(n==h)
                System.out.print(letter[0]);
            }
            System.out.println("");
            }
    int lh=h-1;                             //bottom row of diamond
            for(int i=1;i<=lh;i++)  {                       //which row
                for(int j=1;j<=a;j++) {                     //how many diamonds
                    for(int space=1;space<=i;space++)       //left space
                    {
                    System.out.print(" ");
                    }
                    for(int star=-2*i+(2*h-1);star>0;star--)        //star
                    {
                        if((star==1)||(star==-2*i+(2*h-1)))
                        System.out.print("*");
                        else if((i>=1&&(star==3)||(star==-2*i+(2*h-1)-2)))
                        System.out.print("O");
                        else
                        System.out.print(" ");
                    }
                    for(int space2=1;space2<=i;space2++)    //right space
                        System.out.print(" ");
                }
                System.out.println();
                }
        }
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-30 15:19:32

对当前代码进行最小程度的不言自明的更改(请参见下面带有// CHANGED的行):

大多数情况下,您只需要在这里和那里打印几个额外的空格,而不是总是打印字母数组的第一个元素,即A__。

代码语言:javascript
复制
import java.util.Scanner;

class ManyDiamond {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the height: ");
    int h = sc.nextInt();
    System.out.print("How many diamonds: ");
    int a = sc.nextInt();
    sc.close();
    int lCurr = 0; // CHANGED
    char[] letter = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
        'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
    // top of diamond
    for (int n = 1; n <= h; n++) { // which row
      for (int j = 1; j <= a; j++) { // how many diamond
        for (int k = 1; k <= h - n; k++) { // left space
          System.out.print(" ");
        }
        for (int m = 1; m <= 2 * n - 1; m++) { // star
          if ((m == 1 || m == 2 * n - 1)) {
            System.out.print("*");
          } else if ((n >= 3) && (m == 3) || (m == 2 * n - 3)) {
            System.out.print("O");
          } else {
            System.out.print(" ");
          }
        }
        for (int space1 = 1; space1 <= -n + h; space1++) { // right space
          System.out.print(" ");
        }
        if (n == h && j != a) { // CHANGED
          System.out.print(String.valueOf(letter[lCurr++]) + String.valueOf(letter[lCurr++]) + String.valueOf(letter[lCurr++])); // CHANGED
        } else {
          System.out.print("   "); // CHANGED
        }
      }
      System.out.println("");
    }
    int lh = h - 1; // bottom row of diamond
    for (int i = 1; i <= lh; i++) { // which row
      for (int j = 1; j <= a; j++) { // how many diamonds
        for (int space = 1; space <= i; space++) { // left space
          System.out.print(" ");
        }
        for (int star = -2 * i + (2 * h - 1); star > 0; star--) { // star
          if ((star == 1) || (star == -2 * i + (2 * h - 1))) {
            System.out.print("*");
          } else if ((i >= 1 && (star == 3) || (star == -2 * i + (2 * h - 1) - 2))) {
            System.out.print("O");
          } else {
            System.out.print(" ");
          }
        }
        for (int space2 = 1; space2 <= i; space2++) { // right space
          System.out.print(" ");
        }
        System.out.print("   "); // CHANGED
      }
      System.out.println();
    }
  }
}

示例用法1:

代码语言:javascript
复制
Enter the height: 5
How many diamonds: 4
    *           *           *           *
   * *         * *         * *         * *
  * O *       * O *       * O *       * O *
 * O O *     * O O *     * O O *     * O O *
* O   O *ABC* O   O *DEF* O   O *GHI* O   O *
 * O O *     * O O *     * O O *     * O O *
  * O *       * O *       * O *       * O *
   * *         * *         * *         * *
    *           *           *           *

示例用法2:

代码语言:javascript
复制
Enter the height: 3
How many diamonds: 2
  *       *
 * *     * *
* O *ABC* O *
 * *     * *
  *       *

注意,我把它留给了您,以了解如何使它像示例输出那样双倍行距。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55432646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档