gpt4 book ai didi

c# - 数组值混淆

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:49 24 4
gpt4 key购买 nike

在处理基本的编码挑战时,我遇到了一个令人困惑的情况,我无法理解原因(我是编程新手)

在尝试将数字拆分成各个数字时,我的 int 数组包含值 13 但返回值 49??

这可能有一个明显的原因,如果是这样,我深表歉意。

我找到了另一种方法将我的数字字符串拆分成单个数字,但仍然想知道我做错了什么

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AddTheDigits
{
class Program
{
static void Main(string[] args)
{
using (StreamReader reader = new StreamReader("TextFile1.txt"))
{
//int total = 0;
string line = reader.ReadLine();

//Console.WriteLine(line); //testing

char[] charArray = line.ToCharArray(0,1);

int[] intArray = new int[charArray.Length];

for (int i = 0; i < intArray.Length; i++)
{
intArray[i] = Convert.ToInt32(charArray[i]);
Console.Write(intArray[i]);
}
}
Console.ReadLine();
}
}
}

最佳答案

您可能混淆了数字的 ASCII 代码和数字本身。尝试更换

intArray[i] = Convert.ToInt32(charArray[i]);

intArray[i] = charArray[i] - '0';

看看是否有帮助。

关于c# - 数组值混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31549777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com