using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication8 { class Program { static void Main(string[] args) { ConsoleKeyInfo c; do { System.Console.WriteLine("请输入一个大于100的整数"); int a = 0; string number = Console.ReadLine(); int.TryParse(number, out a); //Console.WriteLine("{0}", a); int[] b = new int[1000]; int cnt = 0; while (a != 0) { b[cnt++] = a % 10; a /= 10; } Console.WriteLine("该整数一共有{0}位", cnt); int sum = 0; Console.Write("实现思路1: 每一位的值为"); for (int i = cnt - 1; i >= 0; --i) { Console.Write("{0}、", b[i]); sum += b[i]; } Console.Write(", 这些位之和为{0}", sum); Console.WriteLine(""); Console.Write("实现思路2: 每一位的值为"); sum = 0; for (int i = 0; i < number.Length; ++i) { Console.Write("{0}、", number[i]); sum += number[i] - ‘0‘; } Console.Write(", 这些位之和为{0}", sum); Console.WriteLine(""); Console.WriteLine("按回车键退出,其他键继续!"); c = Console.ReadKey(); } while (c.Key != ConsoleKey.Enter); } } }
时间: 2024-10-13 09:50:46