本文介绍了Ascii Art字符串到字符的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有输入作为Ascii艺术设计,我希望根据输入的Ascii艺术来打印真实角色.我已经创建了以下程序,但是我没有什么毛病.

I have an input as Ascii art design and i want the real character to be printed based on the inputted ascii art.I have created the below program but i dont whats wrong with it.

     import java.util.*;

 class Ascii {
    static final char START_CHAR = 'a';
    static final char END_CHAR = 'z';
    static final char DELIMITER_CHAR = END_CHAR + 1;

    public static String printchar(char c){
       int l =5,w=4,start=0,end=0;
        String v="";
       c=Character.toLowerCase(c);

       String[] rowArray = new String[5];
       rowArray[0]=" #  ##   ## ##  ### ###  ## # # ###  ## # # #   # # ###  #  ##   #  ##   ## ### # # # # # # # # # # ### ###";
       rowArray[1]="# # # # #   # # #   #   #   # #  #    # # # #   ### # # # # # # # # # # #    #  # # # # # # # # # #   #   #";
       rowArray[2]="### ##  #   # # ##  ##  # # ###  #    # ##  #   ### # # # # ##  # # ##   #   #  # # # # ###  #   #   #   ##";
       rowArray[3]="# # # # #   # # #   #   # # # #  #  # # # # #   # # # # # # #    ## # #   #  #  # # # # ### # #  #  #      ";
       rowArray[4]="# # ##   ## ##  ### #    ## # # ###  #  # # ### # # # #  #  #     # # # ##   #  ###  #  # # # #  #  ###  # ";

      if(START_CHAR <= c && c <= END_CHAR)
      {
        start = (c-START_CHAR)* w;
        end = start+w;
      }

      else
      {
          start=103;end=107;

      }

         for (int i = 0; i < l; i++) {
              v = v+"\n"+rowArray[i].substring(start,end);
          }
    return v;

    }
}

public class Solution {
public static void main(String args[]) {

    String b = Ascii.printchar('A');
    System.out.println(b);
    char c = A.scanChar(b);
    System.out.println("Corresponding Letter ="+c);
        }
}

class A{

static char scanChar(String s)
{
    Ascii.
  Map<String,Character> mapping= new HashMap<>(26);
      mapping.put(" # \n# #\n###\n# #\n# #\n", 'A');
      mapping.put("## \n# #\n## \n# #\n##\n", 'B');
      mapping.put(" ##\n#  \n#  \n#  \n ##\n", 'C');
      mapping.put("## \n# #\n# #\n# #\n## \n", 'D');
      mapping.put("###\n#  \n## \n#  \n###\n", 'E');
      mapping.put(" ###\n#  \n## \n#  \n#   \n", 'F');
      mapping.put("###\n  #\n ##\n   \n # \n", '?');
      System.out.println(mapping.get('A'));
     return mapping.get(s);


    }

    }

有人可以指出错误,因为现在我将字符串与映射值进行比较时得到的是空值?

Can somebody please point out the error as of now i am getting null when comparing the string with the map value ?

推荐答案

 import java.util.*;

 class Ascii {
    static final char START_CHAR = 'a';
    static final char END_CHAR = 'z';
    static final char DELIMITER_CHAR = END_CHAR + 1;

    public static String printchar(char c){
       int l =5,w=4,start=0,end=0;
        String v="";
       c=Character.toLowerCase(c);

       String[] rowArray = new String[5];
       rowArray[0]=" #  ##   ## ##  ### ###  ## # # ###  ## # # #   # # ###  #  ##   #  ##   ## ### # # # # # # # # # # ### ###";
       rowArray[1]="# # # # #   # # #   #   #   # #  #    # # # #   ### # # # # # # # # # # #    #  # # # # # # # # # #   #   #";
       rowArray[2]="### ##  #   # # ##  ##  # # ###  #    # ##  #   ### # # # # ##  # # ##   #   #  # # # # ###  #   #   #   ##";
       rowArray[3]="# # # # #   # # #   #   # # # #  #  # # # # #   # # # # # # #    ## # #   #  #  # # # # ### # #  #  #      ";
       rowArray[4]="# # ##   ## ##  ### #    ## # # ###  #  # # ### # # # #  #  #     # # # ##   #  ###  #  # # # #  #  ###  # ";

      if(START_CHAR <= c && c <= END_CHAR)
      {
        start = (c-START_CHAR)* w;
        end = start+w;
      }

      else
      {
          start=103;end=107;

      }

         for (int i = 0; i < l; i++) {
              v = v+"\n"+rowArray[i].substring(start,end);
          }
    return v;

    }
}

public class Solution {
public static void main(String args[]) {

    String b = Ascii.printchar('Z');
    System.out.println(b);
    System.out.println();
    char c = A.scanChar(b);
    System.out.println("Corresponding Letter = "+c);
        }
}

class A{

static char scanChar(String s)
{
    //s= s.trim();
    Character key=null;
  Map<Character,String> mapping= new HashMap<>();
  mapping.put('A',"\n #  \n# # \n### \n# # \n# # ");
  mapping.put('B',"\n##  \n# # \n##  \n# # \n##  ");
  mapping.put('C',"\n ## \n#   \n#   \n#   \n ## ");
  mapping.put('D',"\n##  \n# # \n# # \n# # \n##  ");
  mapping.put('E',"\n### \n#   \n##  \n#   \n### ");
  mapping.put('F',"\n### \n#   \n##  \n#   \n#   ");
  mapping.put('G',"\n ## \n#   \n# # \n# # \n ## ");
  mapping.put('H',"\n# # \n# # \n### \n# # \n# # ");
  mapping.put('I',"\n### \n #  \n #  \n #  \n### ");
  mapping.put('J',"\n ## \n  # \n  # \n# # \n #  ");
  mapping.put('K',"\n# # \n# # \n##  \n# # \n# # ");
  mapping.put('L',"\n#   \n#   \n#   \n#   \n### ");
  mapping.put('M',"\n# # \n### \n### \n# # \n# # ");
  mapping.put('N',"\n### \n# # \n# # \n# # \n# # ");
  mapping.put('O',"\n #  \n# # \n# # \n# # \n #  ");
  mapping.put('P',"\n##  \n# # \n##  \n#   \n#   ");
  mapping.put('Q',"\n #  \n# # \n# # \n ## \n  # ");
  mapping.put('R',"\n##  \n# # \n##  \n# # \n# # ");
  mapping.put('S',"\n ## \n#   \n #  \n  # \n##  ");
  mapping.put('T',"\n### \n #  \n #  \n #  \n #  ");
  mapping.put('U',"\n# # \n# # \n# # \n# # \n### ");
  mapping.put('V',"\n# # \n# # \n# # \n# # \n #  ");
  mapping.put('W',"\n# # \n# # \n### \n### \n# # ");
  mapping.put('X',"\n# # \n# # \n #  \n# # \n# # ");
  mapping.put('Y',"\n# # \n# # \n #  \n #  \n #  ");
  mapping.put('Z',"\n### \n  # \n #  \n#   \n### ");
     /*char[] ct = new char[s.length()];
     ct= s.toCharArray();
     String l = "\n# # \n# # \n### \n# # \n# # ";
     for(char c:ct)
     System.out.print(c);

     System.out.println();
     System.out.println("Length of L ="+l.length());
     System.out.println("Length of S ="+s.length());
     System.out.println("Equality ="+s.equals(l));
     */
     for(Map.Entry entry: mapping.entrySet())
     {
         if(s.equals(entry.getValue()))
         {
          key = (char)entry.getKey();
          break;
         }
         else{
             key='?';
         }
     }

     return key;

}
}

终于找到解决办法了.

这篇关于Ascii Art字符串到字符的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:19