-5

基本上,我需要编写一个 C# 控制台应用程序,它将采用 3 位数字并执行以下操作:1. 所有 3 位数字的总和(例如,如果数字是 123,那么它将是 6) 2.“重构”他的数字是这样的:百、十、个。示例:365 300+60+5=365 3. 颠倒数字

很多帮助表示赞赏。

4

2 回答 2

0

这是假设您有一个 3 位数的重构部分:

    static void Main(string[] args)
    {
        int num = 365;
        char[] digits = num.ToString().ToCharArray();

        Console.WriteLine(digits.Sum(x=>char.GetNumericValue(x)));
        Console.WriteLine(new string(digits.Reverse().ToArray()));
        Console.WriteLine(string.Format("Hundreds: {0} Tens: {1} Ones: {2}", digits[0], digits[1], digits[2]));

        Console.Read();
    }
于 2013-10-06T01:57:19.420 回答
0

I feel taking risk to answer your question but what the hack..

"refactor" his digits like that: hundreds, tens, ones.

int i = 123, reverse = 0;
while (i > 0)
{
    reverse = (reverse * 10) + (i % 10);
    i /= 10;
}
Console.WriteLine(reverse); //321

sum of all 3 digits (ex. if the number is 123 then it will be 6)

int i = 123, total = 0;
while (i > 0)
{
    total += i % 10;
    i /= 10;
}
Console.WriteLine(total); //6

Thanks but it's not what i meant by saying 'refactor'. For instance, for the input 389 it'll print this: Hundreds: 3 Tens: 8 Ones: 9

int i = 389, houndreds = 0, tens = 0, ones = 0;

ones = i%10;
i /= 10;
tens = i%10;
i /= 10;
houndreds = i%10;

Console.WriteLine("Hundreds: {0} Tens: {1} Ones: {2}", houndreds, tens, ones); //Hundreds: 3 Tens: 8 Ones: 9
于 2013-10-05T22:28:47.120 回答