数字的总和 [英] Sum of digit of the number

查看:109
本文介绍了数字的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int  num,sum =  0 ,r; 
Console.WriteLine( 输入数字:);
num = int .Parse(Console.ReadLine());
while (num!= 0
{
r = num % 10 ;
num = num / 10 ;
sum = sum + r;
}



让我们拿num = 123

r = 123%10 = 12.3,然后如何计算其他两行,我没有得到,你能解释我吗,我是programmimg的新手



我尝试了什么:



i尝试了相同的代码并输出正确,但我无法理解公式如何工作

解决方案

实际 = mod



123%10的结果为3,因为mod等于剩余的。



num 是一个int。所以不能保存任何小数位

int / int的结果总是一个int(四舍五入的四舍五入刚刚丢失)



所以123/10 = 12 





所以这样做是取最后一位数并添加直到没有剩余数字为止:





 sum = 0; 
num = 123;





pass1:

 r = 123%10 = 3 
num = 123/10 = 12
sum = 3



pass2:

< pre lang =text> r = 12%10 = 2
num = 123/10 = 1
sum = 5



pass3:

 r = 1%10 = 1 
num = 123/10 = 0
sum = 6



pass4:

终止为num = 0





我希望有帮助



Andy ^ _ ^


不,模数运算符不会像那样工作。

< pre lang =text>让我们拿num = 123
r = 123%10 = 12.3,

不对:

 r = 123%10 = 3 

模数运算符'%'返回除法的余数,执行123/10 == 12余数3:

如果是模数10,它返回最低有效数字,因为输入数字是十进制数。


当你不明白你的代码在做什么或w如果它完成它的工作,答案是调试器

使用调试器查看代码正在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


int num, sum = 0, r;
 Console.WriteLine("Enter a Number : ");
 num = int.Parse(Console.ReadLine());
while (num != 0)
            {
r = num % 10;
num = num / 10;
sum = sum + r;
}


let we take num = 123
r=123%10=12.3, then how is it calculate other two line, i didn't get, can you explain me,, i am new in programmimg

What I have tried:

i tried the same code and output got correct, but i am unable to understand the how the formula works

解决方案

Actually % = mod

the result of 123 % 10 is 3 as mod equals the remainder only.

num is an int. and so can't hold any decimal places
the result of int / int is always an int (rounded down as the decimal place is just lost)

so 123 / 10 = 12



so what this does is take the last digit and add it to the total until there are no digits left:


sum = 0;
num = 123;



pass1:

r = 123%10 = 3
num = 123/10 = 12
sum = 3


pass2:

r = 12%10 = 2
num = 123/10 = 1
sum = 5


pass3:

r = 1%10 = 1
num = 123/10 = 0
sum = 6


pass4:
terminated as num = 0


I hope that helps

Andy ^_^


No, the modulus operator doesn;t work like that.

let we take num = 123
r=123%10=12.3,

Is not right:

r = 123 % 10 = 3

The Modulus operator '%' returns the remainder of the division, do 123 / 10 == 12 remainder 3:
In the case of modulus 10, it returns the least significant digit, since the input number is in base ten.


When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于数字的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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