简体   繁体   English

总和,数字乘积不等于0与数字的数量…在C#中为SWITCH

[英]Sum,product of digits not equal to 0 and number of digits…WITH SWITCH in C#

I'm new to programming, especially when it comes to C#, but I'm studying it this year and I realize I do like it and really want to comprehend it. 我是编程的新手,尤其是C#,但是我今年正在研究它,我意识到我确实喜欢它并且真的很想理解它。 However, our teacher leaves us to learn it on our own. 但是,我们的老师让我们自己学习。 Ok, no problem with that, the internet is as amazing as it is. 好的,没问题,互联网是如此的令人惊奇。

So I've got this exercise as homework: 因此,我将本练习作为家庭作业:

==== Calculate the sum, product of digits not equal to 0 and the number of digits of an integer.==== ====计算总和,不等于0的位数与整数位数的乘积。====

Thing is, I only know how to make it with do while and if and it works perfectly, but she wants us to do it also with SWITCH and this is where I'm lost because I just don't know how to build the cases (it's fine when case is 0, but how do I write the case when digit or n != to 0?!) 问题是,我只知道如何同时使用if和if并使其完美运行,但她希望我们也可以使用SWITCH进行操作,这就是我迷失的地方,因为我只是不知道该如何构建案件(当case为0时很好,但是当digit或n!=等于0时我怎么写大小写?)

I really need some help with this one and would appreciate sosososo much any help given! 我真的需要一些帮助,非常感谢您给予的任何帮助! Also, could you provide also an explanation? 另外,您能提供一个解释吗? Thank you so much! 非常感谢! :D :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();

You can not print every possible combination in a switch/case but you can at least differ between "0" and "not 0": 您不能在开关/盒中打印所有可能的组合,但是至少可以在“ 0”和“ not 0”之间进行区分:

switch(n)
{
     case 0: // n == 0
         p = 0;
         break;
     default: // this runs in any case but zero
         do
         {
             digit = n % 10;
             s += digit;
             if (digit != 0)
                 p *= digit;
             d++;
             n /= 10;
         } while (n != 0);
         break;
}

Maybe its this, what your teacher wanted to tell you: the default case of a switch, which basically means "everything else". 也许是这样,您的老师想告诉您的是:开关的default情况,基本上意味着“其他所有东西”。

Regarding your analysis of n... Yes of course you can Parse it to an int and to that divison/modulo stuff, but as you are new to programming, maybe you don't know that you can read a string char-by-char through indexing: 关于您对n的分析...当然可以,您可以将其解析为一个int以及该除数/模数的东西,但是由于您是编程的新手,也许您不知道您可以按字符读取字符串-通过索引的char:

string input = Console.ReadLine();
foreach (char c in input)
{
    int digit = Convert.ToInt32(c);
    s += digit;
    p *= digit;
}

This foreach will run through your string char-by-char and store the next character in c . 该foreach将遍历字符串char-by-char并将下一个字符存储在c This code is by far easier to read than your div/mod version. 到目前为止,此代码比div / mod版本更易于阅读。 Easy and clean code helps in understanding. 简洁的代码有助于理解。

When changing it like this, your switch would look like: 像这样更改它时,您的开关将如下所示:

switch (input.Length)
{
    case 0:
        p = 0;
        break;
    default:
        // the foreach loop from above
        break;
}

hope this helps, cheers, Gris 希望这会有所帮助,欢呼,格里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM