我有输入作为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);
}
}
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);
}
}
有人可以指出错误,因为现在我将字符串与映射值进行比较时得到的是空值?
答案 0 :(得分:2)
Coordinate
方法的docs指定
返回指定键所映射到的值;如果此映射不包含键的映射关系,则返回null。
您将ASCII字符作为 key ,而不是 value 。因此,当您尝试获取键“ A”(没有键)时,它将返回null。
更改:
get()
收件人:
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", '?');
答案 1 :(得分:2)
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;
}
}
最后提出解决方案。