为什么我的变量在使用switch语句时不更新?
我是一名学生,我正在尝试制作一个不使用数组的冒险文本游戏。 此代码是一个函数,由为什么我的变量在使用switch语句时不更新?,c,adventure,C,Adventure,我是一名学生,我正在尝试制作一个不使用数组的冒险文本游戏。 此代码是一个函数,由main(在另一个switch case语句中)调用。问题是,这一天不会更新。它仅在代码内联包含在main中时更新 int getArea (int nArea) //area and day { int nShisha, nBall, nLolipop, nPepsi ; int nUser; //test variables int nPrice; // testing variable
main
(在另一个switch case语句中)调用。问题是,这一天不会更新。它仅在代码内联包含在main
中时更新
int getArea (int nArea) //area and day
{
int nShisha, nBall, nLolipop, nPepsi ;
int nUser; //test variables
int nPrice; // testing variable for price
int naUser ;
int nDay;
nDay = 2;
printf("Choose your Area\n");
printf("1. Riyadh\n2. Jeddah\n3. Albaik\n4. Tazaj\n");
printf("5. Go back\n");
scanf("%d", &nArea);
//nArea
do{ //This is for the place
switch (nArea)
{
case 1 :
printf("Riyadh\n");
printf("Day %d of 15\n", nDay);
nDay ++;
if (nDay == 16) {
return 0;
}
getPrice (&nShisha , &nBall, &nLolipop, &nPepsi ) ;// add charge soon
break;
case 2 :
printf("Jeddah\n");
printf("Day %d of 15\n", nDay);
nDay ++;
if (nDay == 16) {
return 0;
}
getPrice (&nShisha , &nBall, &nLolipop, &nPepsi ); // add charge soon
break;
case 3:
printf("AlBaik\n");// add charge soon
printf("Day %d of 15\n", nDay);
nDay ++;
if (nDay == 16) {
return 0;
}
getPrice (&nShisha , &nBall, &nLolipop, &nPepsi );;
break;
case 4:
printf("Tazaj\n");// add charge soon
printf("Day %d of 15\n", nDay);
nDay ++;
if (nDay == 16) {
return 0;
}
getPrice (&nShisha , &nBall, &nLolipop, &nPepsi );;
break;
default : printf("You have entered invalid!");
break;
}
// printf("do you want to go to another place?\n"); //test code change later into better alternative
scanf("%d", &nArea);
}while (nArea!=0); //for area while
}
你们能解释一下为什么不更新吗?根据你们的解释,你们说的是变量
nDay
。在问题中给出的代码中,此变量是函数getArea()
的局部变量。我确信它已在函数中成功更新,但这不会对main()
或其他地方的任何类似命名变量产生影响。这些是同名的不同变量。既然您描述了如何将这段代码从main
分解到它自己的函数中,我很有信心这就是问题的本质
如果希望函数修改调用者的本地变量,则调用者必须传递指向该变量的指针,并且函数必须通过指针间接更新调用者的变量。例如,您可以这样声明getArea()
:
int getArea(int area, int *nDay) {
// ...
然后,您还需要去掉nDay
中getArea()
的另一个声明:
,以及功能体内所有其他出现的nDay
,都需要替换为(*nDay)
*
调用方则需要传递适当的指针,例如:
int main(void) {
int area, rval, nDay;
// ...
rval = getArea(area, &nDay);
// ...
}
*从技术上讲,只有在某些情况下才需要括号,但在其他情况下它们是无害的。用户定义函数中的变量值不会在
main()
函数中更新,只要您不将其返回到main()
函数或变量是从主函数传递的指针。
在代码中,即使变量nDay
已在函数getArea
中更新,但如果不返回该变量,则该变量将不会在main()
函数中更新。你可以用
int getArea (int nArea)
{
...
...
return nDay; // Only this value will be passed to the main() function. nothing else updated here will be passed.
}
int main()
{
int x,y,z, nArea, nDay;//some variable that you declared. Assuming nDay is one of them
...
...
nDay=getArea(nArea); //value returned from the function(which is "nDay" from getArea()) will be assigned to nDay
...
}
另外,当您在函数中覆盖/获取nArea
的新输入(值)时,我看不出有任何理由将nArea
传递到getArea()
。
请注意,来自
main()
的nDay
和来自getArea()
的nDay
实际上是不同的变量,即使它们具有相同的名称。请注意,函数getArea()
最后不返回任何值。此外,它会忽略传递给它的区域
的值,该值会被覆盖,并且提供其值的调用方变量不会受到影响。无法复制:nDay
递增,当nDay==16
时函数返回。请张贴一个,最短的完整代码,显示问题。我已经编辑了传统英语单词选择的问题文本。请检查我是否捕捉到了您的意图,特别是当我将您最初的“我一直这样做”更改为“代码包含在main
”时,当您询问“day”时,您是在谈论变量nDay
?我没有看到任何名为“day”的东西。@JohnBollinger是的,我说的是day,比如你看到的。我是一名球员,他想去“吉达”旅行,但由于某些原因,星期五没有增加。为什么会这样?您认为我需要为getArea()使用指针吗?例如,我使用getArea(int*nday),那么nday会更新吗?还有什么建议吗?我想做一个像《毒品战争》@JohnBollinger这样的文字游戏
int getArea (int nArea)
{
...
...
return nDay; // Only this value will be passed to the main() function. nothing else updated here will be passed.
}
int main()
{
int x,y,z, nArea, nDay;//some variable that you declared. Assuming nDay is one of them
...
...
nDay=getArea(nArea); //value returned from the function(which is "nDay" from getArea()) will be assigned to nDay
...
}