这是问题:
给定两个数字H和N,请使用*
绘制高度为H的N颗钻石。此外,对于N颗钻石中的每一个,也请使用o
绘制另一颗高度为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();
}
}
}
答案 0 :(得分:0)
对当前代码进行最少的不言自明的更改(请参见下面带有// 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 *
* * * *
* *
请注意,我已将其留给您查看,以了解如何使其像示例输出一样以两倍间距排列。