树的ASCII艺术在Java中:

huangapple go评论67阅读模式
英文:

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 &lt; size; line_number++) {
for (int spaces = 0; spaces &lt; size - line_number; spaces++) {
System.out.print(&quot; &quot;);
}
for (int stars = 0; stars &lt; 1 + 2 * line_number; stars++) {
System.out.print(&quot;*&quot;);
}
System.out.print(&quot;\n&quot;);
}
}
public static void second_triangle() {
int size = 15;
for (int line_number = (size - 10); line_number &lt; size; line_number++) {
for (int spaces = 0; spaces &lt; size - line_number; spaces++) {
System.out.print(&quot; &quot;);
}
for (int stars = 0; stars &lt; 1 + 2 * line_number; stars++) {
System.out.print(&quot;*&quot;);
}
System.out.print(&quot;\n&quot;);
}
}
public static void third_triangle() {
int size = 20;
for (int line_number = (size - 10); line_number &lt; size; line_number++) {
for (int spaces = 0; spaces &lt; size - line_number; spaces++) {
System.out.print(&quot; &quot;);
}
for (int stars = 0; stars &lt; 1 + 2 * line_number; stars++) {
System.out.print(&quot;*&quot;);
}
System.out.print(&quot;\n&quot;);
}
}

答案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 &lt; size; row++)
System.out.println(&quot; &quot;.repeat(size - row - 1 + indent) + &quot;*&quot;.repeat(1 + 2 * row));
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row &lt; height; row++)
System.out.println(&quot; &quot;.repeat(indent) + &quot;*&quot;.repeat(width));
}

输出

                   *
***
*****
*******
*********
***********
*************
***************
*****************
*******************
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
*********************
***********************
*************************
***************************
*****************************
*******************************
*********************************
***********************************
*************************************
***************************************
*********
*********
*********
*********

更新

同样的逻辑适用于所有 Java 版本,使用自建的 repeat 方法:

static void printTriangle(int indent, int start, int size) {
for (int row = start; row &lt; size; row++)
System.out.println(repeat(&#39; &#39;, size - row - 1 + indent) + repeat(&#39;*&#39;, 1 + 2 * row));
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row &lt; height; row++)
System.out.println(repeat(&#39; &#39;, indent) + repeat(&#39;*&#39;, width));
}
static String repeat(char c, int count) {
char[] buf = new char[count];
for (int i = 0; i &lt; 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 &lt; size; row++) {
print(&quot; &quot;, size - row - 1 + indent);
print(&quot;*&quot;, 1 + 2 * row);
System.out.println();
}
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row &lt; height; row++) {
print(&quot; &quot;, indent);
print(&quot;*&quot;, width);
System.out.println();
}
}
static void print(String s, int count) {
for (int i = 0; i &lt; 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 &lt; size; row++)
System.out.println(&quot; &quot;.repeat(size - row - 1 + indent) + &quot;*&quot;.repeat(1 + 2 * row));
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row &lt; height; row++)
System.out.println(&quot; &quot;.repeat(indent) + &quot;*&quot;.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 &lt; size; row++)
System.out.println(repeat(&#39; &#39;, size - row - 1 + indent) + repeat(&#39;*&#39;, 1 + 2 * row));
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row &lt; height; row++)
System.out.println(repeat(&#39; &#39;, indent) + repeat(&#39;*&#39;, width));
}
static String repeat(char c, int count) {
char[] buf = new char[count];
for (int i = 0; i &lt; 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 &lt; size; row++) {
print(&quot; &quot;, size - row - 1 + indent);
print(&quot;*&quot;, 1 + 2 * row);
System.out.println();
}
}
static void printSquare(int indent, int width, int height) {
for (int row = 0; row &lt; height; row++) {
print(&quot; &quot;, indent);
print(&quot;*&quot;, width);
System.out.println();
}
}
static void print(String s, int count) {
for (int i = 0; i &lt; count; i++) {
System.out.print(s);
}
}

huangapple
  • 本文由 发表于 2020年9月19日 03:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63961684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定