【问题标题】:C# index is out of bounds with arrayC# 索引超出数组范围
【发布时间】:2021-06-20 01:52:23
【问题描述】:

我正在制作一个“游戏”,您可以在其中使用 Math.PI 输入 pi 的所有 17 位数字。我的想法是,您首先输入 Pi (3.14) 的前 3 位数字,然后继续。

using System;

public class Program
{
   static void Main(string[] args)
   {
        double Pi = Math.PI;
        string Pi_s = Pi.ToString();
        char[] Pi_c = Pi_s.ToCharArray();

        Console.WriteLine("Write the first 3 digits of pi");

        string f = Console.ReadLine();
        char[] e = f.ToCharArray();
        int ind = 3;

        while (ind < 16)
        {

            if(e[ind] == Pi_c[ind]) //here I get "Index was out of bounds with array"
            {
                Console.WriteLine("Congrats you got it right!");
                ind = ind ++;
                Console.WriteLine($"Now, type the first {ind} letters of pi");
            }
            else
            {
                Console.WriteLine("You got something wrong :(");
                break;
            }
        }

        
   }
   
}

我尝试更改 ind 的值,或者尝试在正确设置后将其添加到不同的值,但它不起作用。我认为问题在于您增加了ind 的值,但我不确定。

【问题讨论】:

  • 提示:检查您的输入有多长。
  • P2i 是未声明的变量。应该是 Pi_s?
  • 你让 ind 一直到 15 来索引这些数组,但你从来没有检查过 e 的开始时间,也没有检查 Pi_c 的时间长度。
  • 那么,我应该检查用户输入的数组的长度,如果小于3,我应该让他们再输入一次?

标签: c# arrays indexoutofboundsexception


【解决方案1】:

用这个替换你的 Main() 主体:

        double Pi = Math.PI;
        string Pi_s = Pi.ToString();
        char[] Pi_c = Pi_s.ToCharArray();  //<<<< Fixed this.
        string f = "";

        while (f != "3.14")
        {
            Console.WriteLine("Write the first 3 digits of Pi.");
            f = Console.ReadLine();
            if (f != "3.14")
            {
                Console.WriteLine("You got something wrong :(");
                Console.WriteLine("You must enter the first 3 digits as in 3.14");
            }
            else
            {
                Console.WriteLine("Congrats you got it right!");
            }
        }

        int ind = 4;
        while (ind < 16)
        {
            Console.WriteLine($"Now, type the {ind}th digit of Pi.");
            if (Console.ReadKey().KeyChar == Pi_c[ind])
            {
                Console.Write("\n");
                Console.WriteLine("Congrats you got it right!");
                ind++; //ind = ind++;  <<<<<<< Recommend using the shorthand here.
            }
            else
            {
                Console.Write("\n");
                Console.WriteLine("You got something wrong :(");
            }
        }
        Console.WriteLine("Congrats you WIN!");

这修复了 P2i 错误并执行以下操作:

  1. 循环直到接收到初始的“3.14”输入。
  2. Console.ReadKey() 的形式添加输入接收,以不断获取循环中的下一个字符。
  3. 向程序添加最后一条消息。

我相信这可以回答您的问题。 ;-)

【讨论】:

    【解决方案2】:

    因为数组的索引从 0 开始。如果你包含 3 个数字,因为这个“3.1”数组的最大索引变为 2。但是你从 3 开始“ind”。数组的第三个元素是空的。结果是“错误”。 这是一个真实的版本:

     static void Main(string[] args)
        {
            string Pi_s = Math.PI.ToString();
            char[] Pi_c = Pi_s.ToCharArray();
            Console.WriteLine("Write the first digit of pi");
            char[] array = new char[16];
            int ind = 0;        
            while (ind < 16)
            {
                array[ind] = Convert.ToChar(Console.ReadLine());
                if (array[ind] == Pi_c[ind]) 
                {
                    Console.WriteLine("Congrats you got it right!");
                    ind++;
                    Console.WriteLine($"Now, type the first {ind+1} letters of pi");
                }
                else
                {
                    Console.WriteLine("You got something wrong :(");
                    break;
                }
            }
            Console.ReadKey();
    

    【讨论】:

      猜你喜欢
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 2013-07-05
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多