如果最初输入的值小于 0 或大于 23,我希望用户能够立即重新输入另一个值。目前,我的代码输出“请重新输入小于 24 的值”并停止。然后必须再次运行它,然后用户才能重新输入另一个值。
{
printf ("What is the height of the half-pyramids?\n");
int height = get_int();
if (height>23)
{
printf("please re-enter a value less than 24\n");
}
else if (height<0)
{
printf("please re-enter a value more than 0\n");
}
else
{
printf("%i", height);
}
}
最佳答案
使用循环。
while(1) {
printf ("What is the height of the half-pyramids?\n");
int height = get_int();
if (height>23)
{
printf("please re-enter a value less than 24\n");
}
else if (height<0)
{
printf("please re-enter a value more than 0\n");
}
else
{
printf("%d\n", height);
break; // stop stop looping
}
顺便说一句,使用 %d
打印整数更为惯用,而不是 %i
。请参阅What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)
关于c - 再次自动运行 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46349935/