e心e意

导航

C#学习32-39

30题

namespace 编程100之30题
{
    /*C#编一个程序,用while语句来计算1+1/2+2/3+…+99/100之和*/
    class Program
    {
        static void Main(string[] args)
        {
            double i = 1;
            double result = 1;
            while (i < 100)
            {
                result += i / (i + 1);
                i++;
            }
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

31题

namespace _32题
{
    //矩阵数组
    class Program
    {
        static void Main(string[] args)
        {
            int[][] myArray = new int[2][];
            myArray[0] = new int[5] { 1, 2, 3, 4, 5 };
            myArray[1] = new int[3] { 1, 4, 6 };
            for (int i = 0; i < myArray.Length; i++)
            {
                Console.Write("第<{0}>个数组", i);
                for (int j = 0; j < myArray[i].Length; j++)
                {
                    Console.Write("{0}", myArray[i][j]);
                }
                Console.WriteLine();
                Console.ReadLine();
            }
        }
    }
}

32题

namespace 第33题
{
    class Program
    {
        //给定十个员工的工资,求他们的平均工资
        static void Main(string[] args)
        {
            double all = 0.0;
            double[] salary = new double[] { 1230, 1234, 1500, 1600, 1200, 1400, 1700, 2600, 5000, 4500 };
            foreach (double total in salary)
            {
                Console.WriteLine ("所有人的工资是{0}", total);
                all += total;
            }
            double average = 0.00;
            average = all / 10;
            Console.WriteLine ("average平均工资是:{0}", average);
            int[] i = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 };
            foreach (int j in i)
            {
                Console.WriteLine ("{0}", j);

            }
            int k = Convert.ToInt32 (Console.ReadLine ());
            Console.WriteLine (i[k - 1]);
            Console.ReadKey();
        }
    }
}

33题

namespace 第34题
{
    /*
*输出1-100之间,不重复
*/
    class Program
    {
        static void Main(string[] args)
        {
            int[] intArr = new int[100];
            ArrayList myList = new ArrayList();
            Random rnd = new Random();
            while (myList.Count < 100)
            {
                int j = rnd.Next(100) + 1;
                if (!myList.Contains(j))
                {
                    myList.Add(j);
                }
            }
            for (int k = 0; k < 100; k++)
            {
                intArr[k] = (int)myList[k];
                Console.Write(intArr[k] + "\t");
            }
            Console.ReadKey();
        }
    }
}

34题

namespace _34题
{
    /*// 输入给定字符串,
     * 统计每个字母出现的次数(不区分大小写)*/
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入");
            string str = Console.ReadLine();
            for (int i = 0; i < str.Length; i++)
            {
                int count = 0;
                int flag = 0;
                char ch = str[i];
                for (int j = i - 1; j >= 0; j--)
                {
                    if (str[j] == ch)
                    {
                        flag = 1;
                        break;

                    }
                }
                if (flag == 0)
                {
                    for (int k = 0; k < str.Length; k++)
                    {
                        if (str[k] == ch)
                        {
                            count += 1;
                        }

                    }
                    Console.WriteLine("{0}输出{1}次", ch, count);
                    Console.ReadKey();
                }
            }
        }
    }
}

35题

namespace _36题
{
    /*添加一个重载的构造函数,带有一个String类型的参数,
     * 在此构造函数中将传递的字符串打印出来。
*/
    public class Information
    {
        public Information()
        {
            Console.WriteLine("Information");
        }
        public Information(string info)
        {
            Console.WriteLine(info);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Information info = new Information();
            Information infos = new Information("This is a string.");
            Console.ReadKey();
        }
    }
}

36题

namespace _37题
{
    /*C#名称空间包含一个类和另一个名称空间,
     * 另一个名称空间也有个类,
     * 然后创建一个使用这两个类的应用程序类*/
    public class C0
    {
        static void Main(string[] args)
        {
            n1.c1 _c1 = new n1.c1();
            n1.n2.c2 _c2 = new n1.n2.c2();
            var a = _c1.get();
            var b = _c2.get();
            Console.WriteLine("a={0}",a);
            Console.WriteLine("b={0}",b);
            Console.ReadLine();
        }
    }
}
namespace n1
{
    public class c1
    {
        public int a = 1;
        public int get()
        {
            return a + 1;
        }
    }

    namespace n2
    {
        public class c2
        {
            public int b = 2;
            public int get()
            {
                return b + 1;
            }

        }
    }
}

37题

namespace _38题
{
    /*编写一个使用代表的程序,对整型数组中的元素进行排序。*/
    class Program
    {
        static void Main(string[] args)
        {
        start:
            Console.WriteLine("请输入五个大写字母");
            string str = Console.ReadLine();
            char[] arr = str.ToCharArray();
            if (arr.Length > 5)
            {
                Console.WriteLine("输入错误");
                goto start;
            }
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] >= 'A' && arr[i] <= 'Z')
                {
                    if (i == 4)
                        Console.WriteLine("输入正确");
                }
                else
                {
                    Console.WriteLine("输入错误");
                    goto start;
                }
            }
        }
    }
}

38题

namespace _39题
{
    /*一个控制台应用程序,要求完成写列功能。

1)接收一个整数n。

2)如果接收的值n为正数,输出1~n间的全部整数。

3)如果接收的值n为负值,用break或者return退出程序。

4)转到A继续接收下一个整数。*/
    class Program
    {
static void Main(string[] args)

{

start:

int i = 0;

Console.WriteLine("请输入一个整数N:");

i = int.Parse(Console.ReadLine());

if (i >= 0)

{

Console.Write("1到{0}之间全部的整数为", i);

int j = 1;

while (j <= i)

{

Console.Write(j.ToString() + ' ');

j++;

}

Console.WriteLine();

}

else

{

return;

}

goto start;

}

}
    }

 

posted on 2014-06-19 17:50  e心e意  阅读(108)  评论(0)    收藏  举报