如何在这里循环运行?
How to run while loop here?
好的,这是我的代码,除了while循环之外,一切都工作正常。 该程序的目的是输入一个数字"40235",然后将其除以 10,取其余数 (5(,从主数中减去它,然后除以 10,得到它的完全商,即 4023。我确实设法运行了代码,但是循环没有迭代。(是的,我知道我必须取余数的总和,但首先我需要弄清楚如何迭代 while 循环(。 此循环必须迭代,直到 40235 变为零。
#include <iostream>
using namespace std;
int main()
{
cout<<"Program to yield exact quotient and sum of remainders: "<<endl;
int num, remainder_1, a, subtract;
cout<<"Enter a number: "<<endl
cin>>num;
while(num!=0)
{
remainder_1 =num%10;
num=num-remainder_1;
a =num/10;
cout<<a<<endl;
}
return 0;
}
代码中有多个问题:
- 你的循环条件相反,应该是
while( num != 0 )
- 您无需从 num 中减去提醒,整数除法将处理
- 您不会将
num/10
分配回num
因此,如果您的条件正确,您的循环将永远不会结束。
它应该是这样的:
while( num!=0 )
{
remainder_1 = num%10;
// do something with reminder
num /= 10; // short form of num = num / 10
}
试试
//...
while(num != 0)
{
//...
好吧,想通了。非常感谢大家!
#include <iostream>
using namespace std;
int main()
{
cout<<"Program to yield exact quotient and sum of remainders: "<<endl;
int num, remainder_1, a, subtract;
cout<<"Enter a number: "<<endl;
cin>>num;
int sum;
while(num!=0)
{
remainder_1 =num%10;
num/=10;
cout<<num<<endl;
sum+=remainder_1;
}
cout<<"The Sum is: t"<<sum<<endl;
return 0;
}
相关文章:
- 循环无限运行C++解决骑士之旅问题
- 我的代码运行良好,但在游戏循环中中断
- 如何根据用户在C++中的输入运行不同数量的 for 循环
- 我可以根据用户输入在运行时生成"循环"吗?
- 重新启动后,线程无法在 while 循环中再次运行
- 运行无限循环的最小二叉树问题
- 目标C++,如何在后台线程中使用运行循环?
- 如何在指定的时间内运行循环
- 苹果运行循环的回调方法
- 我怎样才能一直运行循环,直到按下键 (C++)
- 如何在C++中每次运行循环时将变量递增1`
- 如何在第一次运行循环时忽略代码的特定部分
- 如何在每次运行循环时更改for循环中的对象
- 应用程序在访问主运行循环时崩溃 (SIGABRT)
- osX :运行循环选项(创建事件点击时)
- 线程如何运行循环直到连接
- 在指定时间运行c++循环
- 在运行循环外声明、分配和释放线程局部指针变量
- 这是在c++中运行循环的有效方式吗?
- 如何在qt中使用gui对象名运行循环