这是一个简单的问题。
大概代码如下
int a =0;
check(a);
for (int i = 0; i < a; i++)
{
}
public void check(int a)
{
int c = 5;
if (a < c)
{
a = c;
}
}
大概意思是这样,我声明了a,然后调用check函数,这个函数是用来检查a,如果a达到某一个条件,就把a改成其他数值。
但是,在后面的for循环里面,a还是保持原来0的数值,没有变化。
我想请教大家,我要怎么改才能让a的值改变。
代码很简陋,我只是写了大概意思,表达一下这个思路,我就是调用一个函数去处理a,处理完需要把改变后的a传出来。
我试过return a, 但错误提示是a return keyword must not be followed by an object expression
也没办法改成int check(int a),因为not all code paths return a value.
我还是把这个函数的代码贴上来吧
PN = Convert.ToInt32(ReadLine());
CheckAvailable(PN)
public static void CheckAvailable(int PN)
{
int c = 0;
for (int t = 1; t < size; t++)
{
if (newPassenger.SeatChoice[t] == 0)
{
c++;
}
}
if (c == 0)
{
WriteLine(" Our flight is full. ");
PN = 0;
}
else if (PN > c)
{
WriteLine("Sorry, so far, we just have {0} seats available", c);
WriteLine("Please re-enter number of passengers: ");
PN = Convert.ToInt32(ReadLine());
Clear();
while (PN <= 0 || PN > size - 1) //check invalid number
{
WriteLine("The plane only have {0} seats.", size);
WriteLine("Please re-enter correct number.");
PN = Convert.ToInt32(ReadLine());
}
CheckAvailable(PN); //self check again
}
然后下面就是另外一个函数,需要用到更新后的PN。
小弟完全是入门学徒,烦请大家不吝赐教,谢谢各位。
int a =0;
int d=check(a);
for (int i = 0; i < d;i++)
处理。。。。
}
public int check(int a)
{
int c = 5;
if (a < c)
{
a = c;
}
return a;
}