您的位置:首页 > 编程语言 > C#

c# 作业1

2016-03-29 20:51 537 查看
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);

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: