using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace test1
{
class Program
{
//1、 文本文件中存储了多个文章标题、作者,标题和作者之间用若干空格(数量不定)隔开,每行一个,标题有的长有的短,输出到控制台的时候最多标题长度10,如果超过10,则截取长度8的子串并且最后添加“...”,加一个竖线后输出作者的名字。
static void Main(string[] args)
{
//定义文本路径
string path = @"C:\Users\Administrator\.android\Desktop\exam1\exam\test1\txt文件\练习1.txt";
//读取文本信息
string[] articles = File.ReadAllLines(path,Encoding.Default);
//遍历每一行
foreach (string article in articles)
{
//以空格作为分隔符
char[] separator = new char[] { ' ' };
//返回分割后的字符串
string[] lines = article.Split(separator, StringSplitOptions.RemoveEmptyEntries);
//获得分割后的标题
string title = lines[0];
//获得分割后的作者
string author = lines[1];
//判断标题长度是否大于10,如果大于10,则截取长度8的子串并且最后添加“...”
if (title.Length>10)
{
title = title.Substring(0, 8)+"...";
}
//打印输出
Console.WriteLine("{0}|{1}",title,author);
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test2
{
class Program
{
//2、 一个控制台应用程序,要求完成写列功能。1)不断的接收一个整数n。2)如果接收的值n为正数,输出1~n间的全部整数。3)如果接收的值n为负值,用break或者return退出程序。
static void Main(string[] args)
{
//不断的接收一个整数n,设置while(true)
while (true)
{
//提示用户输入一个整数
Console.WriteLine("请输入一个整数");
//接收一个整数n,并判断该数字是否为正数
int n=int.Parse(Console.ReadLine());
Console.WriteLine("1到{0}的全部整数为:",n);
//如果接收的值n为正数,输出1~n间的全部整数
if(n>0)
{
//输出1-n
for(int i=1;i<=n;i++)
{
Console.WriteLine(i);
}
}
//如果接收的值n为负值,用break或者return退出程序。
else
{
break; //或return;
//return;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test3
{
class Program
{
//3、 已知一个int类型的数组,用冒泡排序法将数组中的元素按从小到大的顺序排列;
static void Main(string[] args)
{
//声明一个int类型的数组
int[] arr = { 2,4,3,1,5,9,7,8,6,0};
//定义中间变量
int temp;
//用冒泡排序法将数组中的元素按从小到大的顺序排列
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < i; j++)
{
//如果后一个属主小于前一个数组,则交换
if (arr[j] > arr[i])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//打印输出
Console.WriteLine("用冒泡排序法将数组中的元素按从小到大的顺序排列为");
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0} ",arr[i]);
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test4
{
class Program
{
//4、 定义父亲类Father(姓firstName,财产wealth,血型blood),儿子Son类(玩游戏PlayGame),女儿Daughter类(跳舞Dance),调用父类构造函数给子类字段赋值。
#region 父亲类
/// <summary>
/// 父亲类
/// </summary>
class Father
{
/// <summary>
/// 父亲类的构造函数
/// </summary>
/// <param name="firstName">姓</param>
/// <param name="wealth">财产</param>
/// <param name="blood">血型</param>
public Father(String firstName, decimal wealth, String blood)
{
this.FirstName = firstName;
this.Wealth = wealth;
this.Blood = blood;
}
//姓名
private string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
//财产
private decimal wealth;
public decimal Wealth
{
get { return wealth; }
set { wealth = value; }
}
//血型
private string blood;
public string Blood
{
get { return blood; }
set { blood = value; }
}
#endregion
static void Main(string[] args)
{
//实例化儿子Son类
Son son=new Son("沈",500000,"O");
//打印输出
Console.Write("儿子姓{0},财产{1}人民币,血型是{2}型\t",son.FirstName,son.Wealth,son.Blood);
Console.Write("儿子正在:");
son.PlayGame();
//实例化女儿daughter类
Daughter daughter=new Daughter("沈",400000,"A");
//打印输出
Console.Write("女儿姓{0},财产{1}人民币,血型是{2}型\t", daughter.FirstName, daughter.Wealth, daughter.Blood);
Console.Write("女儿正在:");
daughter.Dance();
Console.ReadKey();
}
#region son类 继承父亲类
/// <summary>
/// son类
/// </summary>
class Son:Father
{
//子类Son的构造函数
public Son(String SfirstName,decimal Swealth,String Sblood):base(SfirstName,Swealth,Sblood)
{
}
//子类Son的PlayGame方法
public void PlayGame()
{
Console.WriteLine("玩游戏PlayGame");
}
}
#endregion
#region Daughter类 继承父亲类
/// <summary>
/// Daughter类
/// </summary>
class Daughter:Father
{
//子类Daughter的构造函数
public Daughter(String DfirstName,decimal Dwealth,String Dblood):base(DfirstName,Dwealth,Dblood)
{
}
//子类Daughter的Dance方法
public void Dance()
{
Console.WriteLine("跳舞Dance");
}
}
#endregion
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test5
{
class Program
{
//5、 从一段文本中提取所有的数字。
static void Main(string[] args)
{
//提醒用户输入文本
Console.WriteLine("从一段文本中提取所有的数字,请输入文本:");
//接收文本信息
string strs = Console.ReadLine();
//遍历文本字符串
foreach (char str in strs)
{
//判断是否为0到9的数字
if (str >= '0' && str <= '9')
{
//输出数字
Console.Write("{0} ",str);
}
}
Console.ReadKey();
}
}
}
//7、 使用Winform编写简单的加法计算器,用户在文本框1、2中输入两个数,点击求和按钮,在文本框3中显示两个数的和。如果1或者2为错误的数据格式,则弹出对话框提示错误.
private void btnSum_Click(object sender, EventArgs e)
{
int a, b;
string a1 = txt1.Text;
string b1 = txt2.Text;
if (string.IsNullOrEmpty(a1)||string.IsNullOrEmpty(b1))
{
MessageBox.Show("请在文本框内输入整数");
return;
}
//判断输入的a是否为整数
if (!(int.TryParse(a1, out a)))
{
MessageBox.Show("请重新在第一个文本框输入整数!");
//清空输入整数的文本信息
txt1.Text = "";
return;
}
//判断输入的b是否为整数
if (!(int.TryParse(b1, out b)))
{
MessageBox.Show("请重新在第二个文本框输入整数!");
//清空输入整数的文本信息
txt2.Text = "";
return;
}
//求和
int sum = a + b;
//将和显示到txtboxSum上
txtboxSum.Text = sum.ToString();
//把输入整数框清空
txt1.Text = "";
txt2.Text = "";
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test8
{
class Program
{
//8、 从学生,老师,校长类中抽象出人的类,学生和老师都有收作业的方法,但是校长不会收作业
static void Main(string[] args)
{
//通过抽象类实现多态
Person president = new President();
Person teacher = new Teacher();
Person student = new Student();
//通过接口实现多态
IhomeWork stuhomework = new Student();
IhomeWork teaHomework = new Teacher();
//校长方法
president.SayHello();
teacher.SayHello();
//老师的方法
teaHomework.doHomework();
student.SayHello();
//学生的方法
stuhomework.doHomework();
Console.ReadKey();
}
}
//抽象人的类
public abstract class Person
{
//抽象SayHello方法
public abstract void SayHello();
}
//定义了一个收作业接口
public interface IhomeWork
{
void doHomework(); // 接口中的方法不能有任何实现
}
//定义一个校长类,不能实现收作业接口
public class President : Person
{
public override void SayHello() //重写父类SayHello方法
{
Console.WriteLine("我是校长");
}
}
//定义一个老师类,实现收作业这个接口
public class Teacher : Person, IhomeWork
{
public void doHomework()//实现收作业接口中的方法
{
Console.WriteLine("老师收作业了");
}
public override void SayHello() //重写父类的SayHello方法
{
Console.WriteLine("我是老师");
}
}
//定义一个学生类,实现收作业这个接口
public class Student : Person, IhomeWork
{
public void doHomework() //实现接口类中的方法
{
Console.WriteLine("学生收作业了");
}
public override void SayHello()
{
Console.WriteLine("我是学生");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace test9
{
//9、 有一个字符串是用空格分隔的一系列整数,写一个程序把其中的整数做如下重新排列打印出来:奇数显示在左侧、偶数显示在右侧。比如‘2 7 8 3 22 9’显示成‘7 3 9 2 8 22’
class Program
{
static void Main(string[] args)
{
string Input = "2 7 8 3 22 9";//初始化整数数组
string[] strs = Input.Split(' ');//以空格作为分隔符
//方法一:ArrayList
ArrayList list1 = new ArrayList();//定义奇数队列
ArrayList list2 = new ArrayList();//定义偶数队列
//遍历数组,判断奇、偶数
foreach (string str in strs)
{
int num;
//如果是奇数,添加到list1奇数队列中
if ((num = Convert.ToInt32(str)) % 2 == 1)
{
list1.Add(num);
}
//如果是奇偶数,添加到list2奇数队列中
else
{
list2.Add(num);
}
}
//将偶数队列合并到奇数队列中
list1.AddRange(list2);
Console.WriteLine("数组{0}奇数显示在左侧、偶数显示在右侧为:",Input);
//遍历新数组输出
foreach (int math in list1)
{
Console.Write(math+" ");
}
//方法二:List<>
//List<int> list1 = new List<int>();//定义奇数队列
//List<int> list2 = new List<int>();//定义偶数队列
//foreach (string str in strs)//遍历数组,判断奇、偶数
//{
// int num;
// if ((num=Convert.ToInt32(str))%2==1)//如果是奇数,添加到list1奇数队列中
// {
// list1.Add(num);
// }
// else//如果是奇偶数,添加到list2奇数队列中
// {
// list2.Add(num);
// }
//}
//list1.AddRange(list2); //将偶数队列合并到奇数队列中
//foreach (int math in list1)//遍历新数组输出
//{
// Console.Write(math+" ");
//}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test10
{
class Program
{
//10、 编写一个掷筛子100次的程序,并打印出各种点数的出现次数。
static void Main(string[] args)
{
//随机数
Random r = new Random();
//初始化筛子6个点数各自出现的次数 为0
int[] Count = { 0, 0, 0, 0, 0, 0 };
Console.WriteLine("掷出的筛子点数为:");
for (int i = 0; i < 5; i++)//输出的列数
{
for (int j = 0; j < 20; j++)//输出的列数
{
int num = r.Next(1, 7);//随机产生1~6之间的点数
//方法一:
Count[num - 1]++;//点数出现次数加1
//方法二:
//if (num == 1) Count[0] += 1;
//if (num == 2) Count[1] += 1;
//if (num == 3) Count[2] += 1;
//if (num == 4) Count[3] += 1;
//if (num == 5) Count[4] += 1;
//if (num == 6) Count[5] += 1;
Console.Write(num + " ");//打印输出掷筛子的点数
}
Console.WriteLine();
}
Console.WriteLine("掷筛子的点数为1出现的次数为:{0}", Count[0]);
Console.WriteLine("掷筛子的点数为2出现的次数为:{0}", Count[1]);
Console.WriteLine("掷筛子的点数为3出现的次数为:{0}", Count[2]);
Console.WriteLine("掷筛子的点数为4出现的次数为:{0}", Count[3]);
Console.WriteLine("掷筛子的点数为5出现的次数为:{0}", Count[4]);
Console.WriteLine("掷筛子的点数为6出现的次数为:{0}", Count[5]);
Console.ReadKey();
}
}
}