java - 使用嵌套的 for 循环绘制圣诞树
问题描述
我正在尝试根据用户输入的高度在控制台中绘制看起来像这样的圣诞树。这里是规则。顶部:矩形/三角形的行数与用户输入的数字一样多。矩形/三角形的宽度比树高度的两倍小一(例如,高度为 5 的树的宽度为 9)。在三角形的情况下,顶部的底部正好在左边距上,并且它上面的每一行都缩进一个空格,并且短两个字符。结果是一个等腰三角形,看起来有点像云杉或枞树的顶部。底部:下面的矩形位于矩形(用于平面树)或三角形(用于圣诞树)下方的中心。它的高度是顶部高度的五分之一多。例如,上面树的矩形有两行,因为 9 ÷ 5 + 1 是 2。矩形的宽度是树宽度的三分之一——但如果宽度相等,则加一。例如,高度为 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 的宽度。
我删除了模块并将其替换为“(高度 -1)/ 5 +1” - 它向上舍入到下一个更大的整数。因此,如果高度为 1、2、3、4 或 5,则为 1(即包括 0:2 行),如果高度为 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:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*******
*******
*******
如果你希望它在任何情况下都很好地居中,你需要稍微作弊并改变茎的宽度。
这样做:我们知道树应该是这样的:
宽度 = 间隙 * 茎宽 + 间隙
从中我们可以很容易地推断出,差距是
间隙 = (with-stemwidth)/2
现在再次插入宽度是:
宽度 = (with-stemwidth)/2 + stemwidth + (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();
}
推荐阅读
- excel - 如何根据单元格值链接到工作簿中的工作表?
- authorization - 将授权信息(如资源、范围、策略和权限)包含到 keycloak JWT 中
- mysql - 优化在聊天应用程序中加载消息历史记录的查询
- reactjs - 使用 JWT 令牌将帖子与 React 和 Django 中的用户相关联
- java - 做反序列化的接收器jvm如何知道序列化时使用了哪个serialuuid
- apache-flink - 当 Flink+Redisson 运行异常,反复重试时,Redis 连接数会不断增长,最终达到上限
- google-api - Google reCaptcha 通过 API 创建凭据
- postgresql - 使用 timescaledb 和 postgresql 12 进行复制时,使用 pg_basebackup 时容器关闭
- pandas - 如何在数据框中的两列中找到部分匹配项
- reactjs - Docusaurus 2 如何在导航栏中添加自定义反应组件