gpt4 book ai didi

c# - 索引超出了数组的范围 c#,莫尔斯转换器

转载 作者:行者123 更新时间:2023-11-30 20:44:24 24 4
gpt4 key购买 nike

我正在尝试构建一个摩尔斯转换器,以下是我的 c# 代码的一部分,但是当我运行它时它告诉我索引超出范围,有人可以修复它吗?我是编程新手:)

private void BTNconvert_Click(object sender, EventArgs e)
{
string input = TBinput.Text;
string[] output=new string[input.Length];
for (int index=0;index<input.Length;index++)
{
index=input.IndexOf('a',index);
output[index]=".-";
}

for (int index = 0; index < input.Length; index++)
{
index = input.IndexOf('b', index);
output[index] = "-...";
}

LBcodes.Text = string.Join(" ",output);

最佳答案

您看到此错误的原因是,如果在字符串中找不到给定的搜索词,IndexOf() 将返回一个 -1 值,因此当你尝试设置 output[-1] 你最终得到一个无效的索引。

关于c# - 索引超出了数组的范围 c#,莫尔斯转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29425834/

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