也许这是一个有明显答案的问题,但我是C的新手,花了几个小时试图让自己工作,并在这个网站上搜索。
我正在构建一个简单的程序,它以一个保存数字(在本例中为1234)的变量开始,程序中的一个选项允许用户更改该变量(值)。
因此,要求用户输入 value
要保持的新号码。然后再问同样的数字。如果输入的数字相同, value
应该更新以保存这些新数字(存储在 newValue
和 newValueCheck
中)。
我试图弄清楚如何更新 value
通过分配它存储在新变量中的数字,但无论我尝试什么,当菜单循环,我要求程序在 value
中的数字,它总是卡为1234。
再次抱歉,如果这是显而易见的,但它让我发疯了。
int x = 1;
int value = 1234;
int enteredValue = 0;
int newValue = 0;
int newValueCheck = 0;
while (x != 0) {
printf("1. Example");
printf("2. Change value");
scanf("%d", &atm);
switch (x) {
Case 1:
//Code here
break;
case 2:
printf("Option 2 selected");
printf("Please enter your new value:");
scanf("%d", &newValue);
printf("Please re-enter your new value:");
scanf("%d", &newValueCheck);
if (newValue == newValueCheck) {
printf("You have successfully changed your value.");
//Code to update 'value' goes here?
}
else {
printf("The values provided do not match. Please try again.");
}
break;
您正在处理"范围"问题。由于在循环中声明了变量,因此它只在该迭代期间"有效"。在循环之前声明它,你应该设置