c - C Replacing Variable Number
问题描述
Is it possible to automate this program more in order to change the variable number?
for (i = 1; i < 4; i++)
{
printf("Enter the Code for Item #%d: ",i);
scanf("%d", &CodeNumber1);
printf("Enter the Price for Item #%d: ",i);
scanf("%f", &Price1);
printf("Enter the Quantity for Item #%d: ", i);
scanf("%d", &Quantity1);
}
So where the variable has 1 written, would it be possible to replace it with i?
Without using an array
解决方案
您可以使用数组而不是修复名称。例如,您可以使用类似的东西
int CodeNumber[4] = {0};
for (i = 0; i < 4; i++)
{
printf("Enter the Code for Item #%d: ",i);
scanf("%d", &CodeNumber[i]);
}
在我看来,更好的方法是使用在内部保存 3 个整数的结构:
struct item {
int CodeNumber;
int ...
}
然后像这样使用它:
struct item myItem[4];
for (i = 0; i < 4; i++)
{
printf("Enter the Code for Item #%d: ",i);
scanf("%d", &(myItem[i].CodeNumber));
...
}
推荐阅读
- ios - 重复符号 '_OBJC_CLASS_$_EXReactNativeUserNotificationCenterProxy' 在:
- php - 将 laravel 5.6 升级到 laravel 8 时我应该改变的观点?
- python - 关于将交易机器人 .exe 文件上传到 VPS 以 24/7 运行的建议
- python - 将 tfa.text.crf_sequence_score 扩展到波束搜索
- git - Gitlab Personal Access Token - 在哪里保存用于无缝克隆/拉/推的令牌
- python - UnboundLocalError at / local variable 'key' 在赋值之前引用
- .htaccess - 使用 .htaccess 重定向编码的 URL
- javascript - SyntaxError:字符串与预期的模式不匹配。D3 querySelectorAll
- java - JAva 找不到符号异常处理
- reactjs - React Hooks 在设置状态的同时处理多个动作的方式是什么?