c#认证考试练习题目

第一章第一题

程序代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 认证汇编第一章第一题
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
Console.WriteLine("请输入一个正整数");
int n =int.Parse(Console.ReadLine());
if (n % 2 == 0)
{
for (int i = 2; i <= n; i=i+2)
{
sum = sum + i;
}
}
if (n % 2 != 0)
{ for (int i = 1; i <= n; i = i + 2)
{
sum = sum + i;
}
}
Console.Write(sum);
Console.WriteLine("输入回车键退出");
Console.ReadKey();

}
}
}

运行结果:

 

 

第一章第十一题:

运行代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 认证汇编第一章第十一题
{
class Program
{
static void Main(string[] args)
{
string s_text; string s_result = null; string s_key;
char ch;
Console.WriteLine("请输入原字符串");
s_text = Console.ReadLine();
Console.WriteLine("请输入密匙字符串");
s_key = Console.ReadLine();
if (s_text.Length != s_key.Length)
Console.WriteLine("密匙字符串长度必须与原字符串长度相等");
for (int i = 0; i < s_text.Length; i++)
{
ch = s_text[i];
s_result +=Convert.ToChar( ch ^ s_key[i]);
}
Console.WriteLine("加密后的字符串为:{0}", s_result);
Console.ReadKey();

}
}
}

执行结果:

 

第二章第一题:

运行代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 认证汇编第二章第一题
{
class Animal
{ private bool m_sex;
private int m_age;
private object get;
private object set;
public string name;
public Animal( )
{
Sex = false;
}
public bool Sex
{
get{return m_sex;}
set{ m_sex =value;}
}
public int Age
{
get { return m_age; }
set { m_age = value;}

}


public virtual void Introduce()
{
if (Sex == true)
Console.WriteLine("This is a male animal");
if (Sex == false)
Console.WriteLine("This is a female animal");

}
public static void show( Animal t)
{
t.Introduce();

}


class Dog:Animal
{

public Dog( )
{
Sex= true;

}
public override void Introduce()
{ if (Sex == true)
Console.WriteLine("This is a male Dog");

if(Sex==false)
Console.WriteLine("This is a female Dog");
}

}
class Cat:Animal
{
public Cat( )
{
Sex = false;

}
public override void Introduce()
{
if (Sex == true)
Console.WriteLine("This is a male Cat");

if (Sex == false)
Console.WriteLine("This is a female Cat");
}
}
static void Main(string[] args)
{
Animal a = new Animal();
Animal.show(a);
Dog b = new Dog();
Animal.show(b);
Cat c = new Cat();
Animal.show(c);

Console.ReadKey();
}
}
}

执行结果:

第二章第十一题:

运行代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 考试汇编第二章第十一题
{
class Animal
{
private bool m_sex;
private string m_sound;
/// <summary>
/// 定义构造函数
/// </summary>
public Animal()
{
Sex = false;
Sound = "Howl";
}
public bool Sex
{
get { return m_sex;}
set { m_sex = value;}
}
public string Sound
{
get { return m_sound; }
set { m_sound = value; }
}
public virtual string Roar()
{
return Sound;
}
/// <summary>
/// 上面是基类
/// </summary>
/// <param name="args"></param>
///
class Dog : Animal
{

/// <summary>
/// Dog派生类
/// </summary>
/// <param name="args"></param>

public Dog()
{
Sex = true;
Sound = "Wow";
}
public override string Roar()
{

return "Dog:" + Sound;
}

}
class Cat:Animal
{
public Cat()
{
Sound = "Miaow";

}
public override string Roar()
{
return "Cat:" + Sound;
}
}
class Cow:Animal
{
public Cow()
{
Sound = "Moo";
}
public override string Roar()
{
return "Cow:" + Sound;
}
}
static void Main(string[] args)
{
Animal animal;
Dog dog = new Dog();
animal = dog;
Console.WriteLine(animal.Roar());
Cat cat = new Cat();
animal = cat;
Console.WriteLine(animal.Roar());
Cow cow = new Cow();
animal = cow;
Console.WriteLine(animal.Roar());
Console.ReadKey();
}
}
}

执行结果:

posted @ 2017-09-19 09:22  天才小熊猫他大舅  阅读(177)  评论(0编辑  收藏  举报