简体   繁体   中英

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. 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.====

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?!)

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

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":

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".

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:

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 . This code is by far easier to read than your div/mod version. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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