C#索引超出了数组的范围-请提供具体帮助

C# Index was outside the bounds of the array - specific help, please
2021-06-10
  •  译文(汉语)
  •  原文(英语)

我正在开发一个用于预订飞机座位的小程序-而且,我不断收到此错误消息.我想让程序向我显示飞机上的哪个座位(flysaeder)正在由哪个乘客(行人)预订.只有,如果我输入的座位多于乘客人数,它将无法运行-我需要它允许开放的座位(比"flysaeder"少"passagerer").我究竟做错了什么?

我对此有些陌生,因此我为您解释不当表示歉意.

在"listeOverPassagerer [index] = listeOverPassagerer [i];"上发生错误.

namespace eksamenvingerne
{
    public partial class Form1 : Form
    {
        int flysaeder;
        int passagerer;
        Random tilfældighed = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            {
                int.TryParse(txtsaeder.Text, out flysaeder);
                int.TryParse(txtantalpassagere.Text, out passagerer);

                if (passagerer > flysaeder)
                {
                    MessageBox.Show("Ingen frie pladser!");
                }

                else
                {
                    int[] listeOverPassagerer = Enumerable.Range(0, passagerer).ToArray();
                    int[] flypladser = new int[flysaeder];

                    for (int i = 0; i < flysaeder; i++)
                    {
                        int index = tilfældighed.Next(0, passagerer);
                        flypladser[i] = tilfældighed.Next(i, passagerer);
                        flypladser[i] = listeOverPassagerer[index];
                        listeOverPassagerer[index] = listeOverPassagerer[i];
                    }

                    for (int i = 0; i < flypladser.Length; i++)
                    {
                        listBox1.Items.Add("Sæde #" + i + ": Passagernr.:" + flypladser[i]); //listboxen udskriver indholdet af hver eneste plads.

                    }
                }
            }
        }
    }
}
速聊1:
您在哪一行收到错误?该错误基本上是在告诉您您正在尝试引用数组中的项目,但该项目实际上不存在.因此,您的逻辑中可能存在错误,对于没有提出该逻辑的人来说,这并不完全明显.哪一行引发错误,发生错误时所涉及的变量的运行时值是多少?
速聊2:
哦对不起.错误出现在"listeOverPassagerer [index] = listeOverPassagerer [i];"上 我认为问题在于索引和数组无法以某种方式匹配,但我不明白.(我实际上不是这个聪明的人).
速聊3:
当passerer == flyseader时会发生吗?
速聊4:
不,它发生在"listeOverPassagerer [index] = listeOverPassagerer [i];"上.
速聊5:
一个提示(不相关的问题):尽量不要使用生僻leters在你的代码,就像ætilfældighed变量名.
解决过程1

您的逻辑实际上是导致此问题的原因:

首先,您要确保 passagerer <= flysaeder

if (passagerer > flysaeder)
{
   MessageBox.Show("Ingen frie pladser!");
}

然后,您可以执行从0到 flysaeder -1

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

但是flysaeder可能比更大,passagerer因此您的访问权限listeOverPassagerer[i]会因为listeOverPassagerer长度太长而引发异常passagerer

速聊1:
这是有道理的-那么我如何允许比"flysaeder"更少的"passagerer"?
速聊2:
int index = tilfældighed.Next(0, passagerer);如此 index = 0..passagerer-1.这看起来不像答案.
速聊3:
不是index造成问题的原因,而是i
速聊4:
是啊,你说得对.但这只是断断续续的逻辑,而不是一成不变的.
速聊5:
我从来没有说过这是一对一的

I'm working on a small programme for booking seats on an airplane - And I keep getting this error. i want the programme to show me which seats on the plane (flysaeder) are being booking by what passenger (passagerer). Only, If I enter in more seats than I have passengers, it won't run - I need it to allow open seats (less "passagerer" than "flysaeder"). What am I doing wrong?

I'm kinda new at this, so I apologize for poor explanation.

Error occurs on "listeOverPassagerer[index] = listeOverPassagerer[i];".

namespace eksamenvingerne
{
    public partial class Form1 : Form
    {
        int flysaeder;
        int passagerer;
        Random tilfældighed = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            {
                int.TryParse(txtsaeder.Text, out flysaeder);
                int.TryParse(txtantalpassagere.Text, out passagerer);

                if (passagerer > flysaeder)
                {
                    MessageBox.Show("Ingen frie pladser!");
                }

                else
                {
                    int[] listeOverPassagerer = Enumerable.Range(0, passagerer).ToArray();
                    int[] flypladser = new int[flysaeder];

                    for (int i = 0; i < flysaeder; i++)
                    {
                        int index = tilfældighed.Next(0, passagerer);
                        flypladser[i] = tilfældighed.Next(i, passagerer);
                        flypladser[i] = listeOverPassagerer[index];
                        listeOverPassagerer[index] = listeOverPassagerer[i];
                    }

                    for (int i = 0; i < flypladser.Length; i++)
                    {
                        listBox1.Items.Add("Sæde #" + i + ": Passagernr.:" + flypladser[i]); //listboxen udskriver indholdet af hver eneste plads.

                    }
                }
            }
        }
    }
}
Talk1:
On which line do you get the error? The error is basically telling you that you're trying to reference an item in an array, but that item doesn't actually exist. So there's likely an error in your logic somewhere, which isn't entirely obvious to someone who didn't come up with that logic. Which line throws the error, and what are the runtime values of the variables involved when it happens?
Talk2:
Oh, sorry. The error is on "listeOverPassagerer[index] = listeOverPassagerer[i];" I believe that the issue is that the index and array doesn't match up somehow, but I don't get it. (I'm actually not the brilliant at this).
Talk3:
Is it happening when passagerer == flyseader?
Talk4:
No, it happens on "listeOverPassagerer[index] = listeOverPassagerer[i];.
Talk5:
One tip (not related to the question): try not to use uncommon leters in Your code, like æ in tilfældighed variable name.
Solutions1

Your logic actually is causing this problem:

First you make sure that passagerer <= flysaeder

if (passagerer > flysaeder)
{
   MessageBox.Show("Ingen frie pladser!");
}

Then you do a for loop from 0 to flysaeder -1

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

But flysaeder might be larger than passagerer hence your access of listeOverPassagerer[i] will throw an exception since listeOverPassagerer is of length passagerer

Talk1:
This makes sense - so how can I allow less "passagerer" than "flysaeder"?
Talk2:
int index = tilfældighed.Next(0, passagerer); so index = 0..passagerer-1 . This doesn't look like the answer.
Talk3:
it's not index that is causing the problem, it's i
Talk4:
Yeah you're right. But that's just broken logic, not an off-by-one.
Talk5:
I never said it's an off-by-one
转载于:https://stackoverflow.com/questions/19081304/c-sharp-index-was-outside-the-bounds-of-the-array-specific-help-please

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入