You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实现字符串反转并转大写的程序输出system.char[]问题求助

问题原因与修复方案

问题根源

你代码里的Convert.ToString(inpChar)用法错误——当参数是char数组时,这个方法不会将数组元素拼接成字符串,而是直接返回数组的类型标识System.Char[],这就是你得到该输出的原因。

修复后的完整代码

将Convert.ToString(inpChar)替换为new string(inpChar),即可正确把char数组转换为字符串。修复后的代码如下:

using System;
public class q2
{
    public static void upperNreverse(string inp)
    {
        char[] inpChar = inp.ToCharArray();
        Array.Reverse(inpChar);
        string inpString = new string(inpChar); // 此处为修复点
        string finalString = inpString.ToUpper();
        Console.WriteLine(finalString);
    }
    public static void Main()
    {
        Console.WriteLine("Please enter the string to convert to uppercase");
        string inp = Console.ReadLine();
        upperNreverse(inp);
    }
}

可选简化写法

你可以合并步骤,让代码更简洁:

public static void upperNreverse(string inp)
{
    char[] inpChar = inp.ToCharArray();
    Array.Reverse(inpChar);
    Console.WriteLine(new string(inpChar).ToUpper());
}

内容的提问来源于stack exchange,提问作者Saad Saleem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:10:47