繁体   English   中英

C# 索引超出数组范围

[英]C# index is out of bounds with array

我正在制作一个“游戏”,您可以在其中使用 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的值,但我不确定。

用这个替换你的 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. 向程序添加最后一条消息。

我相信这回答了你的问题。 ;-)

因为数组的索引从 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM