gpt4 book ai didi

java - 在java中打印菱形+字母的图形

转载 作者:行者123 更新时间:2023-11-30 12:06:40 25 4
gpt4 key购买 nike

问题如下:给定两个数字 H 和 N,请使用 * 绘制 N 个高度为 H 的钻石。此外,对于 N 个钻石中的每一个,还请使用 o 绘制另一个较小的钻石,高度为 H高度 H-2 与前一颗钻石共享同一中心。每个外菱形都有三个连续的英文字母,从A开始依次递增,如下图。

但我只有字母表,结果应该是这样的

      *           *
* * * *
* O *ABC * O *DEF
* * * *
* *

我不知道如何在 * 之后添加字母而不弄乱菱形。

   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();
}
}
}

最佳答案

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

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

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:

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:

Enter the height: 3
How many diamonds: 2
* *
* * * *
* O *ABC* O *
* * * *
* *

请注意,我已将它留给您,让您了解如何使其像示例输出一样双倍行距。

关于java - 在java中打印菱形+字母的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55432646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com