我试图根据用户输入的高度在控制台中绘制看起来像这样的圣诞树。这是规则。
顶部:矩形/三角形的行数与用户输入的数目相同。宽度
矩形/三角形比树的高度小一倍(例如,树的高度
5的宽度为9)。在三角形的情况下,顶部的底部正对着左侧
页边距,其上方的每一行都缩进一个空格,并且短两个字符。的
结果是一个等腰三角形,看起来有点像云杉或枞树的顶部。
底部:下面的矩形居中于矩形(对于“扁平树”)或三角形(对于
圣诞树)。它的高度是顶部高度的五分之一。例如,
上面树的矩形有两行,因为9÷5 +1是2。矩形的宽度是1
树宽的三分之一-如果宽度均匀就加一。例如,
高度为5的三角形的宽度为9,因此矩形的宽度为3(即9÷3)。的树
高度4的底宽为7。其矩形的宽度为3(即7÷3为2,即
甚至将其更改为3)。
How tall should the top of the tree be? 7
Flat Tree:
*************
*************
*************
*************
*************
*************
*************
*****
*****
Xmas Tree:
*
***
*****
*******
*********
***********
*************
*****
*****
目前这是我的代码
TreeStructures.java
import java.util.Scanner;
public class TreeStructures {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int height;
System.out.print("How tall should the top of the tree be? ");
height = scnr.nextInt();
int topWidth = (height * 2) - 1;
int bottomWidth = topWidth/3;
System.out.println();
if (height >= 5 && height <= 20) {
if(bottomWidth % 2 == 0) {
bottomWidth = (topWidth/3) + 1;
}
// FLAT TREE -----------------------------------------
System.out.println("Flat tree:");
// first for loop to print number of rows
for (int i = 1; i <= height; i++) {
// second for loop to print stars to create rectangle
for (int stars = 1; stars <= topWidth; stars++) {
System.out.print("*");
}
// println to print rows in.
System.out.println();
}
// first for loop to print out rows for the bottom part of tree
for (int i = 0; i <= (height / 5) + 1; i++) {
// for loop to print the bottom part of the tree
for (int j = 0; j <= bottomWidth - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j <= bottomWidth - 1; j++) {
System.out.print("*");
}
System.out.println();
}
// XMAS TREE --------------------------------------------
System.out.println("Xmas tree:");
// NESTED LOOPS
// first for loop to print amount of rows
for (int i = 0; i < height; i++) {
// second for loop for print out spaces to match the tree level
for (int j = 1; j < height - i; j++) {
System.out.print(" ");
}
// third for loop to print out stars
for (int k = 0; k < (2 * i + 1); k++) {
System.out.print("*");
}
System.out.println();
}
// first for loop to determine amount of rows for bottom
for (int i = 0; i <= (height / 5); i++) {
// for loop to print the bottom part of the tree
for (int j = 0; j <= bottomWidth - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j <= bottomWidth - 1; j++) {
System.out.print("*");
}
System.out.println();
}
} else {
System.out.println("Sorry, i can only take heights between 5 and 20"
+ "\nQuitting now...");
}
}
}
从现在开始,我已经看完了树,底部也有,但是由于某种原因,数学运算已关闭,我认为它可以正常工作,但是第二件事,我不知道如何为该空间添加空间底部位于树的顶部下方。
我的输出现在看起来像这样。
How tall should the top of the tree be? 8
Flat tree:
***************
***************
***************
***************
***************
***************
***************
***************
*****
*****
*****
Xmas tree:
*
***
*****
*******
*********
***********
*************
***************
*****
*****
但是当我输入其他任何数字(例如6、5、8)时,底部不会居中
最佳答案
当您的茎有1/3时,两侧的空间也是整个宽度的1/3。通用公式为(width-stemWidth)/ 2,在这种情况下,其宽度为1/3。
我删除了模数,然后将其替换为“(height -1)/ 5 +1”-将其舍入到下一个更大的int。因此,如果height为1,2,3,4或5,则得到1(包括0:2行),如果height为6,7,8,9或10,则得到2(包括0:3行)。行)等等。我不确定,但是这就是您想通过模数实现的目标。
import java.util.Scanner;
public class TreeStructures {
static Scanner scnr = new Scanner(System.in);
static int height;
public static void main(String[] args) {
System.out.print("How tall should the top of the tree be? ");
height = scnr.nextInt();
System.out.println();
if (height >= 5 && height <= 20) {
System.out.println("Flat tree:");
flatTree();
System.out.println("Xmas tree:");
xmasTree();
} else {
System.out.println("That's not a valid size. I can only do trees from 5 to 20");
System.out.println("Quitting now.");
}
}
public static void flatTree() {
int width = (height * 2) - 1;
// first for loop to print number of rows
for (int i = 1; i <= height; i++) {
// second for loop to print stars to create rectangle
for (int stars = 1; stars <= width; stars++) {
System.out.print("*");
}
// println to print rows in.
System.out.println();
}
//first for loop to print out rows for the bottom part of tree
for (int i = 0; i <= height / 5; i++) {
if (height % 2 == 0) {
for (int j = 0; j <= ((width) / 3) + 1; j++) {
System.out.print("*");
}
} else {
//second for loop to print out width for the bottom part of the tree
for (int j = 0; j <= (width) / 3; j++) {
System.out.print("*");
}
}
System.out.println();
}
}
public static void xmasTree() {
int width = height * 2 - 1;
// NESTED LOOPS
// first for loop to print amount of rows
for (int i = 0; i < height; i++) {
// second for loop for print out spaces to match the tree level
for (int j = 0; j < height - i; j++) {
System.out.print(" ");
}
// third for loop to print out stars
for (int k = 0; k < (2 * i + 1); k++) {
System.out.print("*");
}
System.out.println();
}
// first for loop to determine amount of rows for bottom
for (int i = 0; i <= (height-1) / 5 +1 ; i++) {
// for loop to print the bottom part of the tree
for (int j = 0; j <= width/3; j++) {
System.out.print(" ");
}
for (int j = 0; j <= (width) / 3; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
输出:
How tall should the top of the tree be? 10
Flat tree:
*******************
*******************
*******************
*******************
*******************
*******************
*******************
*******************
*******************
*******************
********
********
********
Xmas tree:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*******
*******
*******
如果您希望它在任何情况下都能很好地居中,则需要稍微作弊并改变茎的宽度。
为此:
我们知道树应该像这样:
witdh =间隙*茎宽+间隙
由此我们可以轻松推断出差距是
间隔=(with-stemwidth)/ 2
现在再次插入宽度为:
宽度=(with-stemwidth)/ 2 +茎宽+(with with-stemwidth)/ 2
从中我们可以得出:
stemwidth = witdh-2 *((with -stemwidth)/ 2)。
好吧,人们可以评估该权利,并证明这个方程是正确的。
在离散数学中,我们需要考虑余数和余数。
在计算间隙时,除法中会进行四舍五入,并留下了一些剩余部分,这些部分会丢失。
因此,使用上面的公式,我们计算出一个新的茎宽,该茎宽离散地增加,再次使茎宽失去了余数。使它更大一点-但要居中。
public static void xmasTree() {
int width = height * 2 - 1;
int stem = width - 2*width/3;
// NESTED LOOPS
// first for loop to print amount of rows
for (int i = 0; i < height; i++) {
// second for loop for print out spaces to match the tree level
for (int j = 0; j < height - i; j++) {
System.out.print(" ");
}
// third for loop to print out stars
for (int k = 0; k < (2 * i + 1); k++) {
System.out.print("*");
}
System.out.println();
}
// first for loop to determine amount of rows for bottom
for (int i = 0; i <= (height - 1) / 5 + 1; i++) {
// for loop to print the bottom part of the tree
for (int j = 0; j <= width / 3; j++) {
System.out.print(" ");
}
//here we put the formula to use, instead of using width/3, the equivalent is used, that takes rounding into account.
for (int j = 0; j < width - 2*(width/3); j++) {
System.out.print("*");
}
System.out.println();
}
关于java - 使用嵌套的循环绘制圣诞树,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54817778/