在将我的问题标记为重复之前,我已经在研究以下类似的问题和答案:
c : How to signal end of input
Stop a for loop when user is finished entering input in c
根据上述参考问题,我的代码应该是正确的,但是由于某些原因,我无法使它脱离循环并在用户输入int(例如零或减一)时开始打印输出。
出于记录,这是一次学校作业,暂时不允许使用“ fgets”,我们仅限于使用“ scanf”
我真的希望有人可以在这里指出正确的方向。我要做的就是让用户输入未指定数量的int并将它们存储在数组中。我知道允许的最大值数量,这就是为什么我使用数组,但是我想给用户一个选择,可以通过输入零或负一来表示输入结束。我知道它应该非常简单,但是请尝试,除非用户输入字符,否则我将无法使其正常工作。
我的代码如下:
int i = 0;
int j = 0;
int full_sequence[200] = {0};
int numElements = 0;
printf("enter sequence of integers: \n");
for (i = 0; i < 200; i++)
{
if (scanf(" %d", &full_sequence[i]) ==0)
break;
//i've also tried setting i to the maximum index instead of using break, neither of which worked;
else
{
scanf(" %d", &full_sequence[i]);
numElements++;
}
}
for (j = 0; j < numElements; j++)
{
printf("\nend of input *********************");
printf("\nArray element %d is: %d", j, full_sequence[j]);
printf("\nthe number of non-zero elements is: %d", numElements);
}
return 0;
我还尝试过将for循环替换为while循环,并尝试使用负数作为输入结束的信号,如下所示:
while (testValue >= 0)
{
scanf("%d", &testValue);
scanf("%d", &full_sequence[i]);
{
i++;
numElements++;
}
什么也没有,控制台似乎继续期待输入,直到我输入字母字符为止,这时最后退出循环并打印输出。奇怪的是,scanf函数似乎只能捕获我输入的所有其他数字。
如果我尝试重写for循环,以便在将它添加到我的数组之前也利用测试值来验证scanf项,这与我在while循环中的处理方式类似,则如下所示:>
for (i = 0; i < 100; i++)
{
scanf("%d", &testValue);
if (testValue == 0)
break;
else
{
scanf("%d", &full_sequence[i]);
numElements++;
}
}
什么都没有。
答案 0 :(得分:2)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if selectedCellIndex == indexPath.row {
//turn blue
}
else {
//make clear
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let currentCell:dateCollectionCell = collectionViews.cellForItem(at: indexPath) as! dateCollectionCell
selectedCellIndex = indexPath.row
// reload the data to force the call to cellForItemAt
collectionView.reloadData()
}
答案 1 :(得分:1)
由于还有另一个与事物结束编码有关的答案:传统上,用户通过键入文件结尾的控制代码来表示控制台程序中输入结束的信号。在UNIX上就是Control-D,在Linux和OS X上就是Windows,而在Windows上则是control-Z(其前身可以追溯到8位时代)。