我有一个asciiArt从A到Z 打印(使用#)字符的方法。它称为asciiArt.printChar (char c)。
这是一个测验问题,所以我没有此方法的定义。我需要的是获取printChar方法返回的打印字符串,搜索并返回与此字符串匹配的正确字符。
Does this work?
char ScanChar(string art)
{
// Iterate over each character from A to Z.
for (char c = 'A'; c <= 'Z'; c++)
{
// Check to see if the character corresponds with the ASCII art.
if (asciiArt.printChar(c) == art)
{
// Return the character if it does.
return c;
}
}
// Optionally use a null character to indicate that the string passed
// doesn't correspond to any valid ASCII art.
return (char)0;
}
Run Code Online (Sandbox Code Playgroud)
Instead of returning a null character to handle a bad string being passed, you could also throw an error like this.
throw new ArgumentException("The string does not correspond to valid ASCII art.");
Run Code Online (Sandbox Code Playgroud)
Also keep in mind that C# methods are conventionally capitalized.