【问题标题】:Why does this code give an "Index was outside the bounds of the array" error? [duplicate]为什么这段代码会给出“索引超出数组范围”错误? [复制]
【发布时间】:2019-09-25 21:04:12
【问题描述】:

这段代码基本上应该将用户输入拆分为其组件字母,然后输出数组中的第 1、3、5 个字母

bool greater = false;
Console.WriteLine("Enter your name: ");
string userName3 = Console.ReadLine();
while (greater = false)
{
   if (userName3.Length >= 5)
   {
      greater = true;
   }
   else
   {
      Console.WriteLine("The name must be 5 characters or more");
   }
}
string[] userNameArr = userName3.Split();
Console.WriteLine(userNameArr[0] + " " + userNameArr[2] + " " + userNameArr[4]);

当我运行最后一行时会导致错误提示

索引超出了数组的范围

为什么会这样,我该如何解决?

【问题讨论】:

  • 您能否向我们提供您收到此错误的信息
  • 如果,例如,我输入“Warren”或任何大于 5 个字符的输入,我会收到 IndexOutOfRange 错误
  • 字符串默认是不可变的字符数组,你不需要进一步拆分它们

标签: c#


【解决方案1】:

Split() 不会拆分为 char,它通过检测空格字符并将其拆分为字符串数组。

如果要获取字符,通过索引char firstChar = userName3[0];访问输入字符串

Console.WriteLine(userName3[0] + " " + userName3[2] + " " + userName3[4]);

而不是

string[] userNameArr = userName3.Split();
Console.WriteLine(userNameArr[0] + " " + userNameArr[2] + " " + userNameArr[4]);

旁注: 将while (greater = false) 替换为while (!greater)while (greater == false),因为您想进行比较而不是评估

【讨论】:

【解决方案2】:

这应该可行:

bool greater = false;
while (greater == false)
{
   Console.WriteLine("Enter your name: ");
   string userName3 = Console.ReadLine();
   if (userName3.Length >= 5)
   {
      greater = true;
   }
   else
   {
      Console.WriteLine("The name must be 5 characters or more");
   }
}
string[] userNameArr = userName3.Split();
Console.WriteLine(userNameArr[0] + " " + userNameArr[2] + " " + userNameArr[4]);

行:

while (greater = false)

实际上将 false 分配给更大,这意味着您永远不会离开循环。此外,您需要在循环中添加ReadLine,以便再次提示用户

【讨论】:

    【解决方案3】:

    String 是 char 数组,因此要获取特定字符,请使用索引而不是 split()

    例如要从字符串中获取 5 个字母,请使用 userNameArr[4]

    这是从字符串中读取字符的正确方法

        using System;
    
        public class Program
        {
            public static void Main()
            {
                bool greater = false;
                Console.WriteLine("Enter your name: ");
                string userName3 = Console.ReadLine();
                while (greater == false)
                    {
                        if (userName3.Length >= 5)
                            {
                                greater = true;
                            }
                        else
                            {
                                Console.WriteLine("The name must be 5 characters or more");
                            }
                    }
    //Indexing to string is used to get letters from string
                    Console.WriteLine(userName3[0] + " " + userName3[2] + " " + userName3[4]);
            }
        }
    

    实现:DotNetFiddler

    【讨论】:

      【解决方案4】:

      userName3.Split() 默认将用户名拆分为空格,因此如果用户名是phuzi,则拆分将导致数组包含单个项目["phuzi"],则只有userNameArr[0] 有效。任何访问其他内容的尝试都会导致您看到的错误

      【讨论】:

      • 为什么投反对票?
      【解决方案5】:

      发生这种情况是因为数组不包含此索引。 线

      string[] userNameArr = userName3.Split();
      

      你想用什么分割?

      【讨论】:

        猜你喜欢
        • 2022-11-24
        • 2016-08-22
        • 2019-09-04
        • 1970-01-01
        • 2016-09-30
        • 2015-09-12
        • 2017-12-02
        • 1970-01-01
        相关资源
        最近更新 更多