程序按照我的要求计算。 [英] Program Counts To One After What I Want It To.

查看:69
本文介绍了程序按照我的要求计算。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好互联网,

我的程序有问题再次抱歉问了这么多问题。我的问题是,如果我将起始编号设置为2并将增量计数设置为2并将结束编号设置为16,那么我的程序将计数为1,而不是我想要的结果。它最终会计为18。感谢您花时间和看看我的问题。





hello internet,
i have a problem with my program again sorry for asking so many questions. my problem is that my program will count to one for than i want if i set the starting number at two and increment to count by at two and set the end number at 16 it will end up counting to 18. Thanks for taking the time and looking at my question.


#include <stdio.h>
int main(void)
{

unsigned int s;
int i;
int e;

printf("insert starting number: ");
scanf("%d",&s);

printf("insert number to count by: ");
scanf("%d",&i);

printf("insert number to stop counting to: ");
scanf("%d",&e);

while (s<=e)
{
printf("%u\n",s+=i);
}
return 0;
}

推荐答案





问题你有的是while循环的退出条件是< =,在这种情况下s = 16,e = 16.然后在它等于16之后将s递增i。如果你的退出条件是<它应该按照你期望的方式工作。如果增量大于1,则需要小心;它仍然可以通过e。您可能想要检查是否s + i< = e。
Hi,

The problem you are having is that the exit condition of the while loop is <=, and in this case s = 16, and e = 16. You then increment s by i after it is equal to 16. If your exit condition is <, it should work the way you expect. You do need to be careful if your increment is greater than one; it can still put it past e. You might want to check if s+i <= e instead.


这篇关于程序按照我的要求计算。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆