第一章1.int n, i; int result = 0; Console.WriteLine("请输入一个正整数"); n = int.Parse(Console.ReadLine()); if (n % 2 == 1)//判断n是否为奇数 { for (i = 1; i<=n;) { result = result + i; i = i + 2; } } else { for (i = 0; n>i;) { i = i + 2; result = result + i; } } Console.WriteLine(result); Console.Read(); }11.
int i; string s_text, s_key, s_result=null; char ch; Console.WriteLine("请输入原字符串:"); s_text = Console.ReadLine(); Console.WriteLine("请输入密钥字符串:"); s_key = Console.ReadLine(); if (s_text.Length != s_key.Length) { Console.WriteLine("密钥字符串与原字符串长度必须相等"); return ; } for(i=0;i<s_text.Length-1;i++) { ch =Convert.ToChar(s_text[i]^s_key[i]); s_result+=ch; } Console.WriteLine("加密后的字符串为:"); Console.WriteLine(s_result); Console.ReadKey();
第二章1
namespace Test2_1 { class Program { static void Main(string[] args) { Animal ani = new Animal(); Console.WriteLine(ani.Introduce()); Dog dog = new Dog(); Console.WriteLine(dog.Introduce()); Cat cat = new Cat(); Console.WriteLine(cat.Introduce()); Console.ReadKey(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test2_1 { class Animal { bool m_sex; int m_age; 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 string Introduce() { if (Sex) return "This is a male Animal!"; else return "This is a female Animal!"; } } class Dog:Animal { public Dog() { Sex = true; } public override string Introduce() { if (Sex) return "This is a male Dog!"; else return "This is a female Dog!"; } } class Cat : Animal { public Cat() { Sex = false; } public override string Introduce() { if (Sex) return "This is a male Cat!"; else return "This is a female Cat!"; } } }
时间: 2024-10-09 22:22:24