【发布时间】:2021-06-19 01:34:48
【问题描述】:
我想使用 scanf 函数从用户那里获取两个数字或一个数字的输入并将它们放入一个数组中。但是,我不确定如何使用相同的函数来获取数组的一个元素以及数组的两个元素的输入。
即如果用户输入 9 0,它应该能够成功地将它存储在一个数组中并移动到新代码,或者如果用户输入类似 1 的东西,它也应该能够成功地将它存储在数组中,然后移动到新代码。
我已经尝试将 scanf 放入 while 循环中:
int scanned_array[2] = {};
int element = 0;
while(scanf("%d", &scanned_array[element]) {
//... more code here which will have different functions depending on the input...
element++;
}
我怎样才能在只使用 scanf 函数、while 循环、数组和 if 语句的情况下成功地做到这一点?
**编辑: 只是想知道,如果我根据@nmgari 下面的建议更改了我的代码,我怎么能去 if(num == 1) { ... 没有按 ctrl+d?
感谢阅读!
【问题讨论】: