English 中文(简体)
c 在阵列之外排出的排外指数,从来不停。
原标题:c# index outside the bounds of the array, never breaks out of the for-loop

我的方案汇编了起来,但是,如果开端,它永远不会脱离 lo,并且说阵列太大。 这是我的法典:

public class ComparableArray
{   
    public static void Main()
    {
    taxpayer[] taxArray = new taxpayer[5];
    int x;
    for(x= 0; x < taxArray.Length; ++x)
        taxArray[x] = new taxpayer();

        Console.Write("Enter Social Security Number for taxpayer {0}: ", x+1);
        taxArray[x].SSN = Console.ReadLine();
                    Console.Write("Enter gross income for taxpayer {0}: ", x+1);
                    taxArray[x].Income = Convert.ToDouble(Console.ReadLine());
        taxpayer.getRates();
        x++;

    }
}

I m not sure what I m doing wrong, I would appreciate any help.

最佳回答

All the answers were incorrect, thanks for the help though. I was using x twice; once in the for loop and also in the Console.Write, I just changed the for loop to y instead and works great.

int x=0;
int y;
for(y= 0; y < taxArray.Length; ++y){
    taxArray[x] = new taxpayer();

    Console.Write("Enter Social Security Number for taxpayer {0}: ", x+1);
    taxArray[x].SSN = Console.ReadLine();
    Console.Write("Enter gross income for taxpayer {0}: ", x+1);
    taxArray[x].Income = Convert.ToDouble(Console.ReadLine());
    taxpayer.getRates();
    x++;
}
问题回答

You are incrementing x twice in your loop:

在此之后:

for(x= 0; x < taxArray.Length; ++x)

页: 1

x++;

无需在座标上加方括号。 诉讼地已经这样做。

The for loop should be declared a bit differently:

// notice the inclusion of the variable type in the for
// and you want to use x++, not ++x
for(int x= 0; x < taxArray.Length; x++)

But, the real problem is the incrementing of x++ inside the loop. Since the for loop declaration includes an incrementer (x++), there s no need to increment inside the loop as well.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签