簡體   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