ASCII 艺术问题

问题描述 投票:0回答:1

我需要您帮助开发一个处理 ASCII 艺术文本的程序。

程序必须以 ASCII 样式写入一个字母或单词作为输入。

问题是程序最后总是返回以 ASCII 样式写的字母“A”。

你能看一下并告诉我你的想法吗?

代码:

import java.util.*;

class Solution {

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);

        // Read the length (L) of the ASCII style
        int L = in.nextInt();

        // Read the height (H) of the ASCII style
        int H = in.nextInt();
        in.nextLine(); // Pour consommer la ligne vide

        // Read the word to be displayed in ASCII style
        String T = in.nextLine();

        // Read the ASCII Style
        StringBuilder styleBuilder = new StringBuilder();
        for (int i = 0; i < H; i++) {
            String ROW = in.nextLine();
            styleBuilder.append(ROW).append("\n");
        }
        String style = styleBuilder.toString();

        // Display the word using the ASCII style provided        
        String[] lines = style.split("\n");
        int lineCount = lines.length;
        int wordLength = T.length();
        int letterWidth = wordLength * L;// Each letter is assumed to occupy L characters
        for (int i = 0; i < lineCount; i++) {
            StringBuilder printedLine = new StringBuilder();
            for (int j = 0; j < letterWidth; j++) {
                int letterIndex = j / L; // Each letter occupies L characters
                char c = (letterIndex < wordLength) ? T.charAt(letterIndex) : ' '; 
                int index = j % L; // Hint in ASCII style line
                printedLine.append(lines[i].charAt(index));
            }
            System.out.println(printedLine);
        }
    }
}
java ascii ascii-art
1个回答
0
投票

事实并非如此。你想要做的是用 Java 编写 Unix

banner
。默认情况下,适用于 6x8 (wxh) 的网格,其中顶部和底部“行”为空。这是写字母 A 的演示:

public class A {
    public static void main(String[] args) throws Exception {
        final int[][] A = {  { 0, 0, 0, 0, 0, 0 },  { 0, 0, 1, 1, 0, 0 },  { 0, 1, 0, 0, 1, 0 },  { 1, 0, 0, 0, 0, 1 },  { 1, 1, 1, 1, 1, 1 },  { 1, 0, 0, 0, 0, 1 },  { 1, 0, 0, 0, 0, 1 },  { 0, 0, 0, 0, 0, 0 } };

        for(int[] row : A) {
           for (int v : row) {
               if (v == 1) {
                   System.out.print('#');
               }
               else {
                   System.out.print(' ');
               }
           }
           System.out.println();    
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.