如何在这里运行while循环?

huangapple go评论73阅读模式
英文:

How to run while loop here?

问题

这是我的代码,除了while循环之外,一切都正常工作。
这个程序的目的是输入一个数字"40235",然后将它除以10,取余数(5),从主数字中减去它,然后再除以10,得到它的完美商数,即4023。我成功运行了代码,但while循环没有迭代。(是的,我知道我必须取余数的总和,但首先我需要弄清楚如何迭代while循环)。
这个循环必须在40235变为零之前迭代。

#include <iostream>
using namespace std;

int main()
{
    cout << "程序以得到精确商和余数总和:" << endl;
    int num, remainder_1, a, subtract;
    cout << "输入一个数字:" << endl;
    cin >> num;
    
    while(num != 0)
    {
        remainder_1 = num % 10;
        num = num - remainder_1;
        a = num / 10;
        cout << a << endl;
    }
    return 0;
}
英文:

Okay so here is my code, everything is working fine except the while loop.
The purpose of this program is to input a number "40235" and then dividing it by 10, taking it's remainder (5), subtracting it from main number, and then dividing it by 10, to get it it's perfect quotient i.e 4023. I did managed to run the code, but while loop is not iterating. (Yes, I do know I have to take the sum of remainders, but first I need to figure out how to iterate the while loop).
This loop has to iterate until 40235 becomes zero.

#include &lt;iostream&gt;
using namespace std;

int main()
{
    cout&lt;&lt;&quot;Program to yield exact quotient and sum of remainders: &quot;&lt;&lt;endl;
    int num, remainder_1, a, subtract;
    cout&lt;&lt;&quot;Enter a number: &quot;&lt;&lt;endl
    cin&gt;&gt;num;
    
    
    while(num!=0)
    {
        remainder_1 =num%10;
        num=num-remainder_1;
        a =num/10;
        cout&lt;&lt;a&lt;&lt;endl;
    }
    return 0;
}

答案1

得分: 0

尝试

   //...

    当num不等于0时
    {
   //...
英文:

try

   //...

    while(num != 0)
    {
   //...

答案2

得分: 0

你的代码存在多个问题:

  1. 你的循环条件是相反的,应该是 while( num != 0 )
  2. 你不需要从 num 中减去余数,整数除法会处理这个问题
  3. 你没有将 num/10 赋值回 num,所以如果你的条件是正确的,你的循环永远不会结束。

应该像这样:

while( num != 0 )
{
    remainder_1 = num % 10;
    // 对余数进行操作
    num /= 10; // 等同于 num = num / 10
}
英文:

You have multiple problems in your code:

  1. Your loop condition is opposite, it should be while( num != 0 )
  2. You do not need to subtract reminder from num, integer division will take care of that
  3. You do not assign num/10 back to num so if your condition would be right your loop will never end.

It should be something like:

while( num!=0 )
{
    remainder_1 = num%10;
    // do something with reminder
    num /= 10; // short form of num = num / 10
}

答案3

得分: 0

好的,已经弄清楚了。非常感谢,大家!

#include <iostream>
using namespace std;

int main()
{
    cout << "计算确切商和余数总和的程序:" << endl;
    int num, remainder_1, a, subtract;
    cout << "输入一个数字:" << endl;
    cin >> num;
    int sum = 0;
    
    while (num != 0)
    {
        remainder_1 = num % 10;
        num /= 10;
        cout << num << endl;
        sum += remainder_1;
    }
    cout << "总和是: \t" << sum << endl;
    return 0;
}
英文:

Alright, figured it out. Thanks alot, everyone!


#include &lt;iostream&gt;
using namespace std;

int main()
{
    cout&lt;&lt;&quot;Program to yield exact quotient and sum of remainders: &quot;&lt;&lt;endl;
    int num, remainder_1, a, subtract;
    cout&lt;&lt;&quot;Enter a number: &quot;&lt;&lt;endl;
    cin&gt;&gt;num;
    int sum;
    
    while(num!=0)
    {
        remainder_1 =num%10;
        num/=10;
        cout&lt;&lt;num&lt;&lt;endl;
        sum+=remainder_1;
        
    }
    cout&lt;&lt;&quot;The Sum is: \t&quot;&lt;&lt;sum&lt;&lt;endl;
    return 0;
}

huangapple
  • 本文由 发表于 2020年1月4日 12:35:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定