需要帮助搞清楚Java ASCII艺术模式

时间:2014-11-16 00:51:09

标签: java ascii-art

我需要编写一个程序来输出ASCII艺术图案。 模式的大小应根据类常量动态更改。

它应该是这样的:

Number of boxes: 4
Width of boxes: 6
Height of boxes: 3
+------+------+------+------+
|      |      |      |      |
|      |      |      |      |
|      |      |      |      |
+------+------+------+------+

public class testing {
    public static void main(String[] args) {
        for (int height = 1; height <= 2; height++) {
            System.out.println("");
            for (int box = 1; box <= 4; box++) {
                System.out.print("+");
                // System.out.print("|");

                for (int width = 1; width <= 6; width++) {
                    System.out.print("_");
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您需要检查您的循环是否位于&#34; edge&#34;一个盒子并相应地添加不同的字符。

public class testing {
  public static void main(String args[]) {
     int height = 3;
     int width = 6;
     int numberOfBoxes = 4;
     String output = "";
     // height + 2 for the extra row of characters on the top and bottom
     for(int h = 0; h < height + 2; h++ ){
          for(int box = 0; box < numberOfBoxes; box++ ){
              // If on the outer edge, when h = 0 or h = height + 1
              if (h % (height + 1) == 0) {
                  output += "+";
                  for (int w = 1; w <= width; w++){ 
                          output += "-";
                      }
              // Otherwise only draw the vertical lines.
              } else {
                  output += "|";
                  for (int w = 1; w <= width; w++){ 
                          output += " ";
                      }
              }
        }
        // Add the last line of characters
        if (h % (height + 1) == 0) {
            output += "+";
        } else {
            output += "|";
        }
        // Add new line character
        output += "\n";
     }
     System.out.println(output);
  }
}

但是,上面我在输出的末尾添加了很多较小的字符串。相反,使用StringBuilder()更有意义。将字符串添加在一起效率非常低,并创建了许多对象,只能使用一次并抛出。相反(使用StringBuilder()):

public class test {
  public static void main(String args[]) {
   int height = 3;
   int width = 6;
   int numberOfBoxes = 4;
   StringBuilder output = new StringBuilder();
   // height + 2 for the extra row of characters on the top and bottom
   for(int h = 0; h < height + 2; h++ ){
        for(int box = 0; box < numberOfBoxes; box++ ){
            // If on the outer edge, when h = 0 or h = height + 1
            if (h % (height + 1) == 0) {
                output.append("+");
                for (int w = 1; w <= width; w++){ 
                        output.append("-");
                    }
            // Otherwise only draw the vertical lines.
            } else {
                output.append("|");
                for (int w = 1; w <= width; w++){ 
                        output.append(" ");
                    }
            }
        }
        // Add the last line of characters
        if (h % (height + 1) == 0) {
            output.append("+");
        } else {
            output.append("|");
        }
        // Add new line character
        output.append("\n");
   }
   System.out.println(output.toString());
  }
}

答案 1 :(得分:0)

这是一个工作代码,以防它帮助你:)试图将它分成函数,使其更清晰。请注意,上面给出的代码比这个代码更有效,但是这个代码可能更容易理解。

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static int numBoxes  = 4; //Change this to your liking.
static int height =3; //Just how many | will it have.
static int width = 6; //

public static void main (String[] args) throws java.lang.Exception
{

    topBot();
    printHeight();
    topBot();


}

public static void topBot(){
    for(int i = 0; i <= numBoxes*width;i++)
    {
        if(i%width == 0) //When this happens we're in a new box.
            System.out.print("+");
        else
            System.out.print("-");
    }
    System.out.println(); //Move to next line.

}

public static void printHeight(){
    for(int j = 0; j < height;j++){
        for(int i = 0; i <= numBoxes*width;i++)
        {
            if(i%width == 0)
                System.out.print("|"); //Whenever this happens we're in a new box.
            else
                System.out.print(" ");
        }
        System.out.println(); //Move to next line.
    }
}
}

答案 2 :(得分:0)

您可以将每个框表示为一个几乎正方形 String[] 数组:

+------
|      
|      
|      
+------

然后你可以将这些数组逐行拼接起来,并添加一个右边框:

+------+------+------+------+------+
|      |      |      |      |      |
|      |      |      |      |      |
|      |      |      |      |      |
+------+------+------+------+------+

此代码适用于 Java 11

Try it online!

// number of boxes
int n = 5;
// width of boxes
int width = 6;
// height of boxes
int height = 3;
System.out.println("Number of boxes: " + n);
System.out.println("Width of boxes: " + width);
System.out.println("Height of boxes: " + height);
String[] boxes = IntStream.range(0, n)
        // Stream<String>
        .mapToObj(i -> Stream.of(
                // upper border row
                Stream.of("+" + "-".repeat(width)
                        // add a right border to the last box
                        + (i < n - 1 ? "" : "+")),
                // inner part with a left border
                IntStream.range(0, height)
                        .mapToObj(row -> "|" + " ".repeat(width)
                                // add a right border to the last box
                                + (i < n - 1 ? "" : "|")),
                // lower border row
                Stream.of("+" + "-".repeat(width)
                        // add a right border to the last box
                        + (i < n - 1 ? "" : "+")))
                .flatMap(Function.identity())
                .toArray(String[]::new))
        // reduce Stream<String[]> to a single array String[]
        .reduce((box1, box2) -> IntStream.range(0, height + 2)
                .mapToObj(i -> box1[i] + box2[i])
                .toArray(String[]::new))
        .orElse(new String[0]);
// output
Arrays.stream(boxes).forEach(System.out::println);

输出:

Number of boxes: 5
Width of boxes: 6
Height of boxes: 3
+------+------+------+------+------+
|      |      |      |      |      |
|      |      |      |      |      |
|      |      |      |      |      |
+------+------+------+------+------+

另见:How to draw a staircase with Java?