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

时间:2018-09-29 14:34:23

标签: java oop char ascii ascii-art

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

https://i.stack.imgur.com/zPCJV.png

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

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

3 个答案:

答案 0 :(得分:1)

我会这样写:

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();
    }

}

付出时间并尝试了解其工作原理。 输出将是:

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

但是有一个主意。 您应该发明一些算法,为每个字符创建一个位图。

答案 1 :(得分:-1)

这是将数字(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();
        }

    }
}

希望对您有所帮助

答案 2 :(得分:-2)

    class LetterPrinter
    {
      public void print(int letter, int rowNo)
      {
             if(letter==6)
            {
                switch(rowNo)
                {
                   case 0:
                   case 2:
                   case 4:
                        System.out.print("****");break;
                   case 1:
                        System.out.print("*   "); break;
                   case 3:
                        System.out.print("*  *");break;
                }
          }
          else if(letter==0)
          {
            switch(rowNo)
                {
                   case 0:
                   case 4:
                        System.out.print("****");break;

                   default:
                        System.out.print("*  *");break;
                }
          }
      }
}

class Main
{
    public static void main(String[] args)
    {
        LetterPrinter printer=new LetterPrinter();
        for(int i=0;i<=4;i++)
        {
            printer.print(6,i);
            System.out.print("  ");
            printer.print(0,i);
            System.out.print("  ");
            printer.print(6,i);
            System.out.print("\n");
        }
    }
}