猿问

Ascii Art字符串到字符的转换

我有输入作为Ascii艺术设计,我希望根据输入的Ascii艺术来打印真实角色。我创建了以下程序,但是我没有错。


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

        }

}

到目前为止,有人可以指出错误吗?在将字符串与映射值进行比较时,我得到了空值?


慕的地10843
浏览 156回答 2
2回答

智慧大石

该文档的get()方法指定返回指定键所映射到的值;如果此映射不包含键的映射关系,则返回null。您将ASCII艺术作为关键而不是价值。因此,当您尝试获取键“ A”(没有键)时,它将返回null。改变:&nbsp; mapping.put(" # \n# #\n###\n# #\n# #\n", 'A');&nbsp; mapping.put("## \n# #\n## \n# #\n##\n", 'B');&nbsp; mapping.put(" ##\n#&nbsp; \n#&nbsp; \n#&nbsp; \n ##\n", 'C');&nbsp; mapping.put("## \n# #\n# #\n# #\n## \n", 'D');&nbsp; mapping.put("###\n#&nbsp; \n## \n#&nbsp; \n###\n", 'E');&nbsp; mapping.put(" ###\n#&nbsp; \n## \n#&nbsp; \n#&nbsp; &nbsp;\n", 'F');&nbsp; mapping.put("###\n&nbsp; #\n ##\n&nbsp; &nbsp;\n # \n", '?');至:&nbsp; mapping.put('A', " # \n# #\n###\n# #\n# #\n");&nbsp; mapping.put('B' , "## \n# #\n## \n# #\n##\n");&nbsp; mapping.put('C', " ##\n#&nbsp; \n#&nbsp; \n#&nbsp; \n ##\n");&nbsp; mapping.put('D', "## \n# #\n# #\n# #\n## \n");&nbsp; mapping.put('E', "###\n#&nbsp; \n## \n#&nbsp; \n###\n");&nbsp; mapping.put('F', " ###\n#&nbsp; \n## \n#&nbsp; \n#&nbsp; &nbsp;\n");&nbsp; mapping.put('?', "###\n&nbsp; #\n ##\n&nbsp; &nbsp;\n # \n");
随时随地看视频慕课网APP

相关分类

Java
我要回答