回 帖 发 新 帖 刷新版面

主题:简单的字符输出功能

请教:编写一个控制台程序,让用户输入一串字符,最后将其中输入的字符以相反的方向输出?

回复列表 (共4个回复)

沙发

static void Main(string[] args)
        {
            string mystring;
            Console.WriteLine("请输入一个字符串");
            mystring = Console.ReadLine();
            char[] ch=mystring.ToCharArray();
            int i, cd;
            cd = mystring.Length;
            for (i = cd- 1; i >= 0; i--)
            {
                Console.WriteLine(ch[i]);
            }
            Console.ReadKey();
        }
为什么不一排输出呢?

板凳

也是一样的,跟你那个帖子是一样的处理方式:增加一个储存变量。
static void Main(string[] args)
    {
        string mystring;
        string res="";
        Console.WriteLine("请输入一个字符串");
        mystring = Console.ReadLine();
        char[] ch = mystring.ToCharArray();
        int i, cd;
        cd = mystring.Length;
        for (i = cd - 1; i >= 0; i--)
        {
            Console.WriteLine(ch[i]);
            res += ch[i];
        }
        Console.WriteLine(res);
        Console.ReadKey();
    }

3 楼


你太复杂了,其实只要修改我的代码
Console.WriteLing(ch[i]);改为:
Console.Write(ch[i]);
就可以了呀
兄弟

4 楼

用向量是不是简单些呢

我来回复

您尚未登录,请登录后再回复。点此登录或注册