当前位置:  开发笔记 > 编程语言 > 正文

从印刷的asciiArt字符中获取一封信

如何解决《从印刷的asciiArt字符中获取一封信》经验,为你挑选了1个好方法。

我有一个asciiArt从A到Z 打印(使用#)字符的方法。它称为asciiArt.printChar (char c)

这是一个测验问题,所以我没有此方法的定义。我需要的是获取printChar方法返回的打印字符串,搜索并返回与此字符串匹配的正确字符。



1> Kyle Delaney..:

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

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

Also keep in mind that C# methods are conventionally capitalized.

推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有