码迷,mamicode.com
首页 > Windows程序 > 详细

c# 作业1

时间:2016-03-29 21:17:34      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

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);
            

        }
    }
}

 

c# 作业1

标签:

原文地址:http://www.cnblogs.com/linkzijun/p/5334703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!