如何使用 * 字符键入字符串?

How to type a string using * chars?

我被设置为仅使用“*”字符打印一串输入字母 Б О Б。我的 Java 老师希望看到以下输出:

创建 public class Letter 并使用矩阵仅编码两个字母很容易。但是我遇到了制作整个字母表的问题(这是一项额外的任务)。

也许还有另一种方法可以将输入字符串转换为“*”的图片?

我喜欢将数字(0 到 9)转换为 char *,就像您的示例一样:

class JavaApplication57 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Letter> ar = new ArrayList<>();
        System.out.println("Please input string: ");
        String s = sc.next();
        for (char c : s.toCharArray()) {
            ar.add(JavaApplication57.convertToLetter(c));
        }
        for (int i = 0; i < 5; i++) {
            for (Letter l : ar) {
                System.out.print(l.getLine(i) + " ");
            }
            System.out.println("");
        }
    }

    static Letter convertToLetter(char ch) {
        String a = "****",
                b = "*  *",
                c = "   *",
                c2 = "*",
                d = "*   ",
                e = "* * ",
                f = "  * ";

        switch (ch) {
            case '0':
                return new Letter(a, b, b, b, a);
            case '1':
                return new Letter(c2, c2, c2, c2, c2);
            case '2':
                return new Letter(a, c, a, d, a);
            case '3':
                return new Letter(a, c, a, c, a);
            case '4':
                return new Letter(d, e, a, f, f);
            case '5':
                return new Letter(a, d, a, c, a);
            case '6':
                return new Letter(a, d, a, b, a);
            case '7':
                return new Letter(a, c, c, c, c);
            case '8':
                return new Letter(a, b, a, b, a);
            case '9':
                return new Letter(a, b, b, c, a);

        }
        return null;
    }

}

class Letter {

    private String line1, line2, line3, line4, line5;

    public Letter(String line1, String line2, String line3, String line4, String line5) {
        this.line1 = line1;
        this.line2 = line2;
        this.line3 = line3;
        this.line4 = line4;
        this.line5 = line5;
    }

    public String getLine(int i) {
        switch (i) {
            case 0:
                return line1;
            case 1:
                return line2;
            case 2:
                return line3;
            case 3:
                return line4;
            case 4:
                return line5;
            default:
                throw new AssertionError();
        }

    }
}

希望对你有帮助

我会这样写:

import java.util.*;
import java.util.stream.*;

public class Alphabet {

    enum Letters {
        A(
                "   *   ",
                "  * *  ",
                " *   * ",
                "*     *",
                "*******",
                "*     *",
                "*     *"
        ),
        B(
                "*****  ",
                "*     *",
                "*     *",
                "*****  ",
                "*     *",
                "*     *",
                "*****  "
        ),
        C(
                " ***** ",
                "*     *",
                "*      ",
                "*      ",
                "*      ",
                "*     *",
                " ***** "
        ),
        None(
                "*******",
                "*******",
                "*******",
                "*******",
                "*******",
                "*******",
                "*******"
        );

        List<String> bitmap;

        Letters(String... bmp) {
            bitmap = Arrays.asList(bmp);
        }

        List<String> strings() {
            return bitmap;
        }
    }

    private final String SPACE = " ";
    private final String origin;
    private final Map<Character, Letters> bitmaps =
            Stream.of(Letters.values())
            .filter(l -> !l.equals(Letters.None))
            .collect(Collectors.toMap(
                    letters -> letters.toString().charAt(0),
                    letters -> letters
            ));

    public Alphabet(String abc) {
        this.origin = abc;
    }

    List<String> data() {
        List<List<String>> word = origin.chars()
                .mapToObj(c -> bitmaps.getOrDefault((char) c, Letters.None).strings())
                .collect(Collectors.toList());

        return IntStream.range(0, word.get(0).size())
                .mapToObj(idx -> word.stream().map(strings -> strings.get(idx)).collect(Collectors.joining(SPACE)))
                .collect(Collectors.toList());
    }

    void print() {
        List<String> bitmap = data();
        bitmap.forEach(System.out::println);
    }

    public static void main(String[] args) {
        new Alphabet(" ABCBA ").print();
    }

}

花点时间尝试了解它是如何工作的。 输出将是:

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

不过有个想法。 你应该发明一些算法来为每个字符创建一个位图。