我新的节目,尤其是当它涉及到C#,但今年我学习它,我知道我做的喜欢它,真的想要理解它。但是,我们的老师让我们自己学习。好的,没有问题,互联网是如此惊人。总和,数字不等于0,位数产品...在C#开关
所以我有这个练习功课:
====计算的数字不等于0的整数的位数总和,产品====
。事情是,我只知道如何做到这一点,如果它完美地工作,但她希望我们也可以用SWITCH做到这一点,这是我失去了,因为我不知道如何建立案例(这是很好的案件是0,但我怎么写数字或n!= 0的情况?!)
我真的需要一些帮助,并会赞赏sosososo给予任何帮助!另外,你能否提供一个解释?非常感谢! :d
int n, s = 0, p = 1, d = 0, digit;
Console.Write("Number n : ");
n = Convert.ToInt32(Console.ReadLine());
if (n == 0)
p = 0;
do
{
digit = n % 10;
s += digit;
if (digit != 0)
p *= digit;
d++;
n /= 10;
} while (n != 0);
Console.WriteLine("The sum of the digits is: {0} ", s);
Console.WriteLine("The product of the digits not equal to 0 is : {0} ", p);
Console.WriteLine("The number of the digits is: {0}", d);
Console.ReadKey();
如果我们有办法赶上未在上市的情况下,开关构造... – Funk
有趣的是,这至少是第二个问题与相同的话题(数字乘积...)我今天读了...切换在这里解释:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch – Michael
说你的老师你想用Linq :)'string str =“12345”; var s = str.Sum(x => x - '0'); var p = str.Select(x => x - '0')。Aggregate((y,i)=> y * = i);' –