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");
}
}
}
}
运行结果: