C# 基础语法小练习

1. 通过控制台接受输入的数字,放入一维数组并实现数组进行翻转数据处理,然后将翻转后的数据显示在控制台。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1{

        class Program{

            static void Main(string[] args){

                string N = Console.ReadLine();

                char[] strarr = N.ToString().ToCharArray();

                StringBuilder sb = new StringBuilder();

                for (int i = strarr.Length - 1; i >= 0; i--){

                    sb.Append(strarr[i]);

                }

                Console.WriteLine(sb.ToString());

                Console.ReadKey();

            }

        }

    }

运行结果:

2. 通过控制台接受输入的长方形的二条边长,求出长方形的面积并将结果显示在控制台屏幕上。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1{

        class Program{

            static void Main(string[] args){

                int w,h = 0;

                Console.Write("请输入长:");

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

                Console.Write("请输入宽:");

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

                Console.Write("面积是:{0}",w*h);

                Console.ReadKey();

            }

        }

    }

运行结果:

3. 在控制台接受用户输入的数字,然后将数字使用冒泡排序的方法进行排序,并将排序后的结果输出到控制台显示。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1{

        class Program{

            static void Main(string[] args){

                int[] num = new int[10];

                Console.WriteLine("请输入十个数:");

                for (int i = 0; i < 10; i++){

                    string s = Console.ReadLine();

                    int x = int.Parse(s);

                    num[i] = x;

                }

                int j;

                int n;

                for (j = 0; j < 10; j++){

                    for (n = 0; n < 9 - j; n++){

                        if (num[n] > num[n + 1]){

                            int m;

                            m = num[n];

                            num[n] = num[n + 1];

                            num[n + 1] = m;

                        }

                    }

                }

                for (n = 0; n < 10; n++){

                    Console.WriteLine(num[n]);

                }

                Console.ReadKey(false);

            }

        }

    }

运行结果:

4. 定义一个雇员类Employee,其中包括一个静态域TotalSalary,实现该类的静态构造函数及实例构造函数,实现输出全部雇员薪水的功能。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1{

    public class Employee{

        public static double TotalSalary { get; set; }

        public double Salary { get; set; }

        public string EmployeeName { get; set; }

        public Employee(string empName, double salary){

            this.EmployeeName = empName;

            this.Salary = salary;

            Employee.TotalSalary += salary;

        }

        static Employee(){   TotalSalary = 0D;   }

        public static double GetTotalSalarys(){   return TotalSalary; }

        public double GetSalary(){

            return this.Salary;

        }

        public override string ToString(){

            return String.Format("{0} 的薪水为:{1}", this.EmployeeName, this.Salary);

        }

        class Program{

            static void Main(string[] args){

                Employee[] emp = new Employee[3];

                emp[0] = new Employee("张三", 5600.25);

                emp[1] = new Employee("李四", 8500.00);

                emp[2] = new Employee("王五", 15000.88);

                Console.WriteLine("员工的总薪水为:{0}\n", Employee.GetTotalSalarys());

                Console.WriteLine("{0} 的薪水为:{1}\n", emp[0].EmployeeName, emp[0].Salary);

                Console.WriteLine(emp[1].ToString());

                Console.WriteLine("\n{0} 的薪水为:{1}\n", emp[2].EmployeeName, emp[2].Salary);

                Console.ReadKey();

            }

        }

    }

}

运行结果:

5. 编写一个控制台程序,该程序包括3个类,其中Animal是父类,Dog和Cat是派生于Animal的子类,定义显示各种动物类的叫声。然后,创建者3个类的对象,依次显示着3个对象的类型和叫声。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication2{

    class Program{

        static void Main(string[] args){

            List<Animal> animals = new List<Animal>();

            Animal cat = new Cat();

            Animal dog = new Dog();

            Console.WriteLine("\n下面将显示的是动物的叫声\n");

            animals.Add(cat);

            animals.Add(dog);

            foreach (Animal animal in animals){

                animal.shout();

            }

            Console.ReadKey();

        }

        class Animal{

            public virtual void shout() { }

        }

        class Cat : Animal{

            public override void shout(){

                Console.WriteLine("这是猫的叫声:喵喵~~\n");

            }

        }

        class Dog : Animal{

            public override void shout(){

                Console.WriteLine("这是狗的叫声:汪汪~~\n");

            }

        }

    }

}

运行结果:

转载于:https://my.oschina.net/xieganying/blog/788450

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值