簡體   English   中英

我在 C# 中遇到 System.ArgumentOutOfRangeException 錯誤?

[英]Im getting System.ArgumentOutOfRangeException error in C#?

我正在做一項學校作業,我必須將 8 位二進制數轉換為十進制數。 這是我的代碼;

         private void btnCalculate_Click(object sender, EventArgs e)
        {
            string invoer = txtBoxInput.Text;

            // binair naar decimaal
            if (rdBtnBinair.Checked == true)
            {
                int invoerlengte = invoer.Length;
                double nieuwgetal = 0;
                for (int i = 0; i <= invoerlengte; i++)
                {
                    if (int.Parse(invoer.Substring(i, 1)) == 1)
                    {
                        nieuwgetal += Math.Pow(2,(i+1));
                    }
                }
                txtBoxOutput.Text = nieuwgetal.ToString();

在 C# 中。 我得到錯誤:System.ArgumentOutOfRangeException:索引和長度必須引用字符串中的位置。

認為我的代碼應該不錯,看不出有什么問題。 問候

你可能想改變這個:

for (int i = 0; i <= invoerlengte; i++)

對此:

for (int i = 0; i < invoerlengte; i++)

因為當i等於invoer.Length時,下一行代碼將尋找字符串末尾之后的下一個字符,這超出了范圍:

invoer.Substring(i, 1)

這是因為您的 foreach 循環一次迭代太多,因為您使用<=而不是 '<'。

在這些情況下,檢查何時發生什么總是好的。 您可以使用調試器或普通的舊Console.WriteLine (或 debug.log w/e)來執行此操作

在線示例: https://dotnetfiddle.net/UyE2rw

    public static void Main()
    {
        var invoer = "12345";
        int invoerlengte = invoer.Length;
                double nieuwgetal = 0;
                for (int i = 0; i <= invoerlengte; i++)
                {
                    Console.WriteLine("i: " + i + ", substring: " + invoer.Substring(i, 1));

                    if (int.Parse(invoer.Substring(i, 1)) == 1)
                    {
                        nieuwgetal += Math.Pow(2,(i+1));
                    }
                }

    }

輸出:

i: 0, substring: 1
i: 1, substring: 2
i: 2, substring: 3
i: 3, substring: 4
i: 4, substring: 5
Run-time exception (line 13): Index and length must refer to a location within the string.
Parameter name: length

Stack Trace:

[System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length]
   at System.String.Substring(Int32 startIndex, Int32 length)
   at MyApp.Main() :line 13

請注意 - 索引從 0 開始,從 1 開始計數。因此 substring(0,1) 是索引 0 處的字符,但它是字符串數組中的第一個元素 (char)。

item1 在 index0,item2 在 index1 等等。

德groetjes!

private void btnCalculate_Click(object sender, EventArgs e)
        {
            string invoer = txtBoxInput.Text;
            int invoerlengte = invoer.Length;
            double nieuwgetal = 0;
            if (rdBtnBinair.Checked == true)
            {
                for (int i = 1; i <= invoerlengte; i++)
                {
                    if (int.Parse(invoer.Substring(invoerlengte - (1 * i), 1)) == 1)
                    {
                        nieuwgetal += Math.Pow(2, (i - 1));
                    }
                }
            }

            txtBoxOutput.Text = nieuwgetal.ToString();

        }

這是解決方案,現在可以使用。 再見!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM