我正在尝试用一个用户输入的三角形顶部和一个矩形底座来构建一棵圣诞树,但是我在构建底座时遇到了困难。规格如下:三角形以单个*开头,每行增加*,每行增加2个,使得最后一行为2(n)-1,其中n为输入的高度。三角形。树下面的矩形(树桩)的高度等于1加(1/5)n,树桩的宽度是1 / 3n但是如果1 / 3n的结果是偶数,则将总数加1树桩宽度。 (即树桩宽度总是奇数。) 这是我到目前为止,树的三角形部分很好,但矩形是一团糟。
public class xmasTree {
public static final int TREE_MAXIMUM = 20;
public static final int TREE_MINIMUM = 3;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//User introduction
System.out.println("This program prints a \"Christmas\" tree. You "
+ "choose how big the tree will be (within reason);
// prompt for and get user input
System.out.println("How tall should the top of the tree be?");
int treeHeight = keyboard.nextInt();
while (treeHeight < TREE_MINIMUM || TREE_MAXIMUM < treeHeight) {
System.out.println("That's not a valid size. I can only do trees "
+ "from 3 to 20.");
System.out.println("Qutting now.");
System.exit(0);
}
//Print the top triangle section of the christmas tree
for (int i = 0; i < treeHeight; i++) {
for (int j = 0; j < treeHeight - i; j++) {
System.out.print(" ");
}
for (int k = 0; k < (2 * i + 1); k++) {
System.out.print("*");
}
System.out.println();
}
//Print the bottom rectangle section of the tree
for (int c = 0; c <= (1 + (treeHeight / 5)); c++) {
for (int d = 0; d <= (treeHeight / 3); d++) {
System.out.print("");
}
System.out.printf("*");
}
}
}
如果有人可以帮我弄清楚如何获得适合矩形的正确输入形状,我知道一旦正确构建,我可以用简单的printf将其居中。
答案 0 :(得分:0)
首先,您需要想象您的结果应该是什么。
示例n = 8
,因此基数应为1 + n/5 = 2
高,n / 3 = 2
调整为3
宽。
*
***
*****
*******
*********
***********
*************
***************
***
***
您的代码存在以下问题:
print("")
是一个毫无意义的陈述。它什么都不做。*
计数等于基本宽度。println()
,所以基本高度肯定不会起作用。你让树的部分很好,所以你真的不应该有这个问题。我不会给你答案,但希望这个可视化的例子可以帮助你自己做。
答案 1 :(得分:0)
如果你让你的for循环从0开始,那么就不要使用等号,因为它会比你期望的还要多工作一次
答案 2 :(得分:0)
你几乎成功了,只是关于你的代码的几条评论:
在内心,你应该打印出*,在外面为你打印一条新线
扫描仪键盘=新扫描仪(System.in);
//User introduction
System.out.println("This program prints a \"Christmas\" tree. You "
+ "choose how big the tree will be (within reason)");
// prompt for and get user input
System.out.println("How tall should the top of the tree be?");
int treeHeight = keyboard.nextInt();
while (treeHeight < TREE_MINIMUM || TREE_MAXIMUM < treeHeight) {
System.out.println("That's not a valid size. I can only do trees "
+ "from 3 to 20.");
System.out.println("Qutting now.");
System.exit(0);
}
//Print the top triangle section of the christmas tree
for (int i = 0; i < treeHeight; i++) {
for (int j = 0; j < treeHeight - i; j++) {
System.out.print(" ");
}
for (int k = 0; k < (2 * i + 1); k++) {
System.out.print("*");
}
System.out.println();
}
//Print the bottom rectangle section of the tree
int stumpHeight = (1 + (treeHeight / 5));
int stumpWidth;
if ((treeHeight / 3) % 2 == 0) {
//it is even add 1
stumpWidth = (treeHeight / 3) + 1;
} else {
//it is odd keep it like that
stumpWidth = (treeHeight / 3);
}
for (int c = 0; c < stumpHeight; c++) {
//sorry for adding the spaces but I don't like the incomplete answers
//centering
for (int d = 0; d < treeHeight - stumpWidth/2; d++) {
System.out.print(" ");
}
//adding stump
for (int d = 0; d < stumpWidth; d++) {
System.out.print("*");
}
//going to the next line
System.out.println();
}