我的程序编译但是当它运行时它永远不会脱离循环并且说数组太大了。这是我的代码:
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++;
}
}
我不确定我做错了什么,我将不胜感激。
答案 0 :(得分:5)
你在循环中递增x两次:
来到这里:
for(x= 0; x < taxArray.Length; ++x)
并且在这里:
x++;
答案 1 :(得分:2)
您不需要在循环体中递增循环计数器x++;
。循环已经做到了。
答案 2 :(得分:2)
应该声明for循环有点不同:
// 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++)
但是,真正的问题是循环中x++
的递增。由于for
循环声明包含一个增量器(x ++),因此也不需要在循环内增加。
答案 3 :(得分:0)
所有答案都不正确,谢谢你的帮助。我两次使用x;一旦在for循环中,也在Console.Write中,我只是将for循环更改为y并且效果很好。
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++;
}