英文:
Tree ASCII Art in Java
问题
public class christmas_tree{
public static void main (String[] args) {
first_triangle();
second_triangle();
third_triangle();
//tree_base();
}
public static void first_triangle() {
int size = 10;
for (int line_number = 0; line_number < size; line_number++) {
for (int spaces = 0; spaces < size - line_number; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < 1 + 2 * line_number; stars++) {
System.out.print("*");
}
System.out.print("\n");
}
}
public static void second_triangle() {
int size = 15;
for (int line_number = (size - 10); line_number < size; line_number++) {
for (int spaces = 0; spaces < size - line_number; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < 1 + 2 * line_number; stars++) {
System.out.print("*");
}
System.out.print("\n");
}
}
public static void third_triangle() {
int size = 20;
for (int line_number = (size - 10); line_number < size; line_number++) {
for (int spaces = 0; spaces < size - line_number; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < 1 + 2 * line_number; stars++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
英文:
im writing a code where i need to use 3 loops, one must be a double nested loop, to create an asciiart of anything I would like. I'm trying to make a christmas tree, and once i broke it down into segments, i realize im kind of stuck and not sure how to proceed.
public class christmas_tree{
public static void main (String[] args) {
first_triangle();
second_triangle();
third_triangle();
//tree_base();
}
public static void first_triangle() {
int size = 10;
for (int line_number = 0; line_number < size; line_number++) {
for (int spaces = 0; spaces < size - line_number; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < 1 + 2 * line_number; stars++) {
System.out.print("*");
}
System.out.print("\n");
}
}
public static void second_triangle() {
int size = 15;
for (int line_number = (size - 10); line_number < size; line_number++) {
for (int spaces = 0; spaces < size - line_number; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < 1 + 2 * line_number; stars++) {
System.out.print("*");
}
System.out.print("\n");
}
}
public static void third_triangle() {
int size = 20;
for (int line_number = (size - 10); line_number < size; line_number++) {
for (int spaces = 0; spaces < size - line_number; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < 1 + 2 * line_number; stars++) {
System.out.print("*");
}
System.out.print("\n");
}
}
答案1
得分: 1
以下是代码的翻译部分:
作为未来学习的灵感,使用 Java 11 方法 [`repeat()`][1],我相信可以使用这么简单的代码来打印圣诞树:
```lang-java
public static void main(String[] args) {
printTriangle(10, 0, 10);
printTriangle(5, 5, 15);
printTriangle(0, 10, 20);
printSquare(15, 9, 4);
}
static void printTriangle(int indent, int start, int size) {
for (int row = start; row < size; row++)
System.out.println(" ".repeat(size - row - 1 + indent) + "*".repeat(1 + 2 * row));
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row < height; row++)
System.out.println(" ".repeat(indent) + "*".repeat(width));
}
输出
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
*********************
***********************
*************************
***************************
*****************************
*******************************
*********************************
***********************************
*************************************
***************************************
*********
*********
*********
*********
更新
同样的逻辑适用于所有 Java 版本,使用自建的 repeat
方法:
static void printTriangle(int indent, int start, int size) {
for (int row = start; row < size; row++)
System.out.println(repeat(' ', size - row - 1 + indent) + repeat('*', 1 + 2 * row));
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row < height; row++)
System.out.println(repeat(' ', indent) + repeat('*', width));
}
static String repeat(char c, int count) {
char[] buf = new char[count];
for (int i = 0; i < count; i++)
buf[i] = c;
return new String(buf);
}
或者,也许更接近预期的方式,使用 for
循环和 print
调用,但将简单的打印循环移到可重用的助手方法中,以避免多次重复相同的代码,并提高代码正在做什么的清晰度:
static void printTriangle(int indent, int start, int size) {
for (int row = start; row < size; row++) {
print(" ", size - row - 1 + indent);
print("*", 1 + 2 * row);
System.out.println();
}
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row < height; row++) {
print(" ", indent);
print("*", width);
System.out.println();
}
}
static void print(String s, int count) {
for (int i = 0; i < count; i++) {
System.out.print(s);
}
}
<details>
<summary>英文:</summary>
As an inspiration for your future learning, using Java 11 method [`repeat()`][1], I believe the Christmas tree can be printed with code that is this simple:
```lang-java
public static void main(String[] args) {
printTriangle(10, 0, 10);
printTriangle(5, 5, 15);
printTriangle(0, 10, 20);
printSquare(15, 9, 4);
}
static void printTriangle(int indent, int start, int size) {
for (int row = start; row < size; row++)
System.out.println(" ".repeat(size - row - 1 + indent) + "*".repeat(1 + 2 * row));
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row < height; row++)
System.out.println(" ".repeat(indent) + "*".repeat(width));
}
Output
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
*********************
***********************
*************************
***************************
*****************************
*******************************
*********************************
***********************************
*************************************
***************************************
*********
*********
*********
*********
UPDATE
Same logic that will work in all Java versions, by using a home-built repeat
method:
static void printTriangle(int indent, int start, int size) {
for (int row = start; row < size; row++)
System.out.println(repeat(' ', size - row - 1 + indent) + repeat('*', 1 + 2 * row));
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row < height; row++)
System.out.println(repeat(' ', indent) + repeat('*', width));
}
static String repeat(char c, int count) {
char[] buf = new char[count];
for (int i = 0; i < count; i++)
buf[i] = c;
return new String(buf);
}
Or perhaps, closer to the intended way(?), using for
loop with print
calls, but moving the simple print loops to reusable helper method, to void repeating the same code many times, and to improve the clarity of what the code is doing:
static void printTriangle(int indent, int start, int size) {
for (int row = start; row < size; row++) {
print(" ", size - row - 1 + indent);
print("*", 1 + 2 * row);
System.out.println();
}
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row < height; row++) {
print(" ", indent);
print("*", width);
System.out.println();
}
}
static void print(String s, int count) {
for (int i = 0; i < count; i++) {
System.out.print(s);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论