我不明白这个程序是如何工作的,如果原始和设置为0
,然后r(remainder)
被加到总和0
。这两位数字的正确和是多少?我觉得这应该是这样的... sum = num + r;
c#请解释它为什么有效,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Application
{
class MainClass
{
public static void Main (string[] args)
{
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;**
}
Console.WriteLine("Sum of Digits of the Number : "+sum);
Console.ReadLine();
}
}
}
'r'从号码中取出数字。并将其添加到总和中。混乱在哪里? –
欢迎来到[SO],请阅读[FAQ]和[问]。祝你好运! –