c++ - 将两个整数之间的数字相加的 C++ 程序
问题描述
这是我的代码,它在 x < y 时有效。但不是当 x > y 时。我怎样才能解决这个问题?我知道问题出在 while 循环内,我尝试了许多不同的方法,但无法解决问题。
int main()
{
int x,y;
int total = 0;
cout<<"Please give me an integer: ";
cin >> x;
cout<<"Please give me another integer: ";
cin >> y;
int counter = x;
while(counter <= y ){
total += counter;
++counter;
}
cout << "The total of the numbers " << total<<endl;
}
解决方案
推荐阅读
- python - 如何从 Pytorch 张量中去掉每一列都用零填充?
- c++ - 我应该如何释放不使用“new”关键字创建的对象的内存?
- debugging - 在例程中间调用地址
- metal - 如何使用 Metal 在渲染图像上绘制形状而不重绘整个纹理?
- r - 如何为堆积条形图中的两列着色
- android - 无法从 Firebase 检索数据
- android - 为什么微调器项目样式有效,但微调器下拉项目样式失败?
- javascript - Edge 中的 OIDC 客户端问题
- javascript - ASP.NET 和 C#:在键入和粘贴时从 asp.TextBox 中删除空格
- firebase - 编译的 Jar 不断抛出: SynchronizationContext 中未捕获的异常