C#基础入门典型例题(2)

来源:互联网 发布:ipad登陆淘宝卖家中心 编辑:程序博客网 时间:2024/05/17 03:02
可能上次的题目题目有些不利于新手,以后以5或6道题为一组,供大家进行练习。大家可以先做试试看,再看下面的答案。

1输入十个数,获取他的最大数,并获取是第几个输入的数字
2.输入一个整数,判断是否为闰年(闰年:四年一闰,百年不闰,四百年再闰)
3.输入一个5位数,获取他各个位置的数字
4.输入一个整数,输出它的阶乘(例如5!=5*4*3*2*1)
5.输入十个数,获取他们的第二大的数 (不使用数组)

1输入十个数,获取他的最大数,并获取是第几个输入的数字

public void Topic1()        {            int max = 0, temp = 0;                      //假定最大的数为max,我们还需要一个记录输入次数的数字temp            for (int i = 0; i < 10; i++)            {                int n = Convert.ToInt32(Console.ReadLine());    //循环输入10个数字                if (n > max)                {                    max = n;                                //如果输入的数字比max大,则将将该值赋值给max                    temp = i + 1;                           //其中i为输入数字的次数,然而i以0起始,所以temp的值要+1                }            }            Console.WriteLine("最大的数字为{0},是输入的第{1}个数字",max,temp);        }

2.输入一个整数,判断是否为闰年(闰年:四年一闰,百年不闰,四百年再闰)

public void Topic2()        {            Console.WriteLine("请输入想要查询的年份");            int year = Convert.ToInt32(Console.ReadLine());            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))      //主要掌握if条件从句中&和|的用法,&表示两个条件都必须满足,而|只需满足一个即可                Console.WriteLine("{0}为闰年", year);            else                Console.WriteLine("{0}不为闰年", year);        }

执行结果:
这里写图片描述

3.输入一个5位数,获取他各个位置的数字

public void Topic3()        {            int n = Convert.ToInt32(Console.ReadLine());            if (n > 9999 && n < 100000)                 //判断输入的数字是否为五位数            {                int n1 = n / 10000;                int n2 = (n - 10000 * n1) / 1000;                int n3 = (n - 10000 * n1 - 1000 * n2) / 100;                int n4 = (n - 10000 * n1 - 1000 * n2 - 100 * n3) / 10;                int n5 = n - 10000 * n1 - 1000 * n2 - 100 * n3 - 10 * n4;                Console.WriteLine("输入的五位数万位为{0},千位为{1},百位为{2},十位为{3},个位为{4}", n1, n2, n3, n4, n5);            }            else                Console.WriteLine("请输入一个五位数");        }

上面的方法比较简单,下面来一个升级版的,输入一个多位数,输出他们各个位置的数字。

    Console.WriteLine("请输入一个数字");            int n = 0;                                           //n来记录字符串中数字读取的位置            string num = Console.ReadLine();                    //我们把数字当作字符串,这里读取的是一个字符串            string[] s = {"个","十","百","千","万","十万","百万","千万","亿" };         //将各个单位储存在数组中,稍后调用            for (int i = num.Length;i>0; i--)            {                int j = i - 1;                             //这里的J读取的是输入数字的单位                Console.WriteLine("输入的多位数{0}位为{1}", s[j], num.Substring(n, 1));                n++;                //Substring的用法,括号内第一个数字是从第几个字符开始读取,第二个数字是读取几位                //比如 输入123  Substring(0,1)的意思是从第0位开始,读取一个字符的意思 输出结果为1            }            

运行结果:
这里写图片描述
4.输入一个整数,输出它的阶乘

Console.WriteLine("请输入一个整数数");            int a = Convert.ToInt32(Console.ReadLine());            double result = 1;            Console.Write("{0}!=", a);            for (int i = a; i > 0; i--)            {                Console.Write("{0}*", i);                if (i == 1)                    Console.Write("1");                result = result * i;            }            Console.WriteLine("此数的阶乘为{0}", result);

5.输入十个数,获取他们的第二大的数 (不使用数组)

 int max = 0, max1 = 0;            for (int i = 0; i < 10; i++)            {                int n = Convert.ToInt32(Console.ReadLine());                if (n > max)                {                    max1 = max;                    max = n;                }                if (n > max1 && n < max)                {                    max1 = n;                }            }            Console.WriteLine("最大数为{0},第二大的数为{1}", max, max1);

希望对大家有所帮助。

0 0