1.设计一个Ticket类,有一个距离属性(本属性只读,在构造方法中赋值),不能为负数,有一个价格属性,价格属性只读,并且根据距离计算价格(1元/公里):-----------
0-100公里 票价不打折101-200公里 总额打9.5折201-300公里 总额打9折300公里以上 总额打8折有一个方法,可以显示这张票的信息.业务:
不需要设计太多,只需要 提示用户 拥有的票的种类,然后让用户选择,选择后 按照价格打折后 显示给用户强调:只需要两个类(Ticket 票据类,Saler 售票员类)Ticket票据类
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace S01Morning{ ////// 票据类 /// public class Ticket { ////// 票据的编号 种子 /// public static int idSeed = 1; ////// 票据编号 /// public int id; ////// 距离 /// public int distance; ////// 出发地 /// public string startLocation; ////// 目的地 /// public string endLocation; public Ticket(int dis, string start, string end) { this.id = idSeed++; this.distance = dis; this.startLocation = start; this.endLocation = end; } #region 1.0 价格 + int Price() ////// 价格 /// ///public int Price() { return distance * 1; } #endregion #region 2.0 显示票据内容 + void Show() /// /// 显示票据内容 /// public void Show() { Console.WriteLine("编号:{0},出发地:{1} -> 目的地:{2},票价:{3}", this.id, this.startLocation, this.endLocation, this.Price()); } #endregion #region 2.1 根据 优惠 输出打折后的价格信息 +void ShowWithDaZhe(float rate) ////// 2.1 根据 优惠 输出打折后的价格信息 /// /// 折率 public void ShowWithDaZhe(float rate) { Console.WriteLine("编号:{0},出发地:{1} -> 目的地:{2},票价:{3}", this.id, this.startLocation, this.endLocation, this.Price() * rate); } #endregion }}
Saler售货员
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace S01Morning{ ////// 售票员 /// public class Saler { ////// 票据数组 /// Ticket[] list; #region 构造函数 初始化票据 ////// 构造函数 初始化票据 /// public Saler() { list = new Ticket[10]; list[0] = new Ticket(600, "广州", "长沙"); list[1] = new Ticket(150, "广州", "深圳"); list[2] = new Ticket(1900, "广州", "北京"); list[3] = new Ticket(1000, "广州", "台北"); list[4] = new Ticket(40, "广州", "东莞"); list[5] = new Ticket(800, "广州", "丽江"); list[6] = new Ticket(3000, "广州", "拉萨"); list[7] = new Ticket(3000, "广州", "东京"); list[8] = new Ticket(10000, "广州", "巴黎"); list[9] = new Ticket(7000, "广州", "莫斯科"); } #endregion #region 0.0 开始工作 +void StartWork() ////// 0.0 开始工作 /// public void StartWork() { do{ Selling(); Console.WriteLine("\n是否要继续购买呢?(y/n)"); }while(Console.ReadLine()=="y"); } #endregion #region 1.0 卖票 +void Selling() ////// 1.0 卖票 /// public void Selling() { while (true) { //1.0 显示 当前的票 ShowTickts(); Console.WriteLine("您要买去哪的票呢?"); int tId = int.Parse(Console.ReadLine().Trim()); //2.0 根据用户选择 票据编号 获取 票据对象 Ticket t = GetTicketById(tId); if (t != null) { Console.WriteLine("恭喜您购买成功了如下车票:"); if (t.distance > 0 && t.distance <= 100)//0-100公里 票价不打折 { t.Show(); } else if (t.distance > 100 && t.distance <= 200)//101-200公里 总额打9.5折 { t.ShowWithDaZhe(0.95f); } else if (t.distance > 200 && t.distance <= 300)//201-300公里 总额打9折 { t.ShowWithDaZhe(0.9f); } else//300公里以上 总额打8折 { t.ShowWithDaZhe(0.8f); } break; } else { Console.WriteLine("对不起,您输入的编号有误,请重新输入!"); } } } #endregion #region 1.1 显示票据信息 -void ShowTickts() ////// 1.1 显示票据信息 /// private void ShowTickts() { //循环票据数组 for (int i = 0; i < list.Length; i++) { Ticket t = list[i]; t.Show(); } } #endregion #region 1.2 根据id 查找票据对象 -Ticket GetTicketById(int ticketId) ////// 1.2 根据id 查找票据对象 /// /// id ///票据对象 private Ticket GetTicketById(int ticketId) { for (int i = 0; i < list.Length; i++) { Ticket t = list[i]; if (t.id == ticketId) { return t; } } return null; } #endregion }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace S01Morning{ class Program { ////// 阅读代码:找到 程序的入口! /// /// static void Main(string[] args) { //1.0 创建售票员 Saler s = new Saler(); //2.0 开始工作 s.StartWork(); Console.ReadLine(); } }}
2.猜拳游戏---------------
先接收一个玩家的名字,玩家和机器人猜拳接收玩家的 输入(1-石头;2-剪刀;3-布)机器人 随机生成 1-3之间的数值两个数值 按照规则 进行比较,最终显示输赢!玩家类(Player)、机器人类(Robot)、裁判类(Judge)
//-----------------------------------------------1.当 程序员 规定 一组数据(石头、剪刀、布) 用 【数值类型】来标识 的时候,需要严格的【限制】 这组 数值 的【取值范围】! 且 对这组数据 要求 有明确 的 【可读性】! 且 提供 给程序员 明确便捷的 操作语法!不容易写错! 解决方案: 【枚举-Enum】 1.1枚举的定义语法: public enum FistType { /// <summary> /// 枚举值:石头 /// </summary> Rock = 1,/// <summary>
/// 枚举值:剪刀 /// </summary> Scissors = 2,/// <summary>
/// 枚举值:布 /// </summary> Cloth = 3 } 1.2枚举类型 变量 的访问 FistType type = FistType.Rock; type = FistType.Scissors; 1.3可以直接将 枚举类型 作为 方法的 返回值类型 或者 参数类型2.在C#中,class后跟的 是类名,但其只是 整个类的名字 的一部分而已!
真正的完整的类名 是 命名空间.类名!如:namespace A{ public class DongGuan { }}类名:DongGuan类的全名称:A.DongGuan
出拳类型 枚举
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace S02猜拳{ ////// 出拳类型 枚举 /// 枚举的本质 就是 用代码的枚举值 来 替代 对应的 数值! /// public enum FistType { ////// 枚举值:石头 /// Rock = 1, ////// 枚举值:剪刀 /// Scissors = 2, ////// 枚举值:布 /// Cloth = 3 }}
工具帮助类
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace S02猜拳{ ////// 工具类 /// public class Helper { #region 1.0 获取用户输入的一个 整型数值 +int GetAUserNum() ////// 1.0 获取用户输入的一个 整型数值 /// ///public static int GetAUserNum(string strMsg) { int num=-1; while (true) { Console.Write(strMsg); string strNum = Console.ReadLine(); if (int.TryParse(strNum, out num)) { break; } else { Console.WriteLine("请输入数值!"); } } return num; } #endregion }}
裁判类
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace S02猜拳{ ////// 裁判类 /// public class Judger { #region 1.0 机器人 判定 两个输入 的输赢 void Judge(int playerNum, int robotNum) ////// 1.0 机器人 判定 两个输入 的输赢 /// /// 玩家输入 /// 机器人输入 public void Judge(FistType playerFistType, FistType robotFistType) { //1.0 如果相等,则是平局 if (playerFistType == robotFistType) { Console.WriteLine("平局~~!"); } else//2.否则,对各种情况进行判断 { // 石头1 吃 剪刀2 吃 布3 吃 石头 //直接 用 两个 枚举值 进行 相减运算 (实际运算的 是 两个枚举值对应的 数值) int num = playerFistType - robotFistType; if (num == -1 || num == 2) { Console.WriteLine("玩家赢了~~~~"); } else { Console.WriteLine("机器人赢了~~~~"); } } } #endregion }}
玩家类
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace S02猜拳{ ////// 玩家类 /// public class Player { public string name; public Player(string name) { this.name = name; } #region 1.0 玩家进行 猜拳 +int Play() ////// 1.0 玩家进行 猜拳 /// ///public FistType Play() { while (true) { int num = Helper.GetAUserNum("输入值(1-石头;2-剪刀;3-布):"); if (num >= 1 && num <= 3) { //if (num == 1) // return FistType.Rock; //else if (num == 2) // return FistType.Scissors; //else // return FistType.Cloth; return (FistType)num;//直接将 接收到 的 1~3 的一个数值 转成对应的枚举值! } else { Console.WriteLine("数值必须在 1-3之间!"); } } } #endregion }}
机器人类
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace S02猜拳{ ////// 机器人 类 /// public class Robot { Random ran = new Random(); #region 1.0 机器人返回 猜拳编号 +int Play() ////// 1.0 机器人返回 猜拳编号 /// ///public FistType Play() { int num = ran.Next(1, 4);// 1 <= x < 4 FistType type = (FistType)num;//将 数值 转成对应的 枚举值 switch (type) { case FistType.Rock: { Console.WriteLine("机器人出的 是 【石头】!"); break; } case FistType.Scissors: { Console.WriteLine("机器人出的 是 【剪刀】!"); break; } default: { Console.WriteLine("机器人出的 是 【布】!"); break; } } return type; } #endregion }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace S02猜拳{ class Program { /* 先接收一个玩家的名字,玩家和机器人猜拳 接收玩家的 输入(1-石头;2-剪刀;3-布) 机器人 随机生成 1-3之间的数值 两个数值 按照规则 进行比较,最终显示输赢! 玩家类(Player)、机器人类(Robot)、裁判类(Judger) */ static void Main(string[] args) { //1.创建玩家 (接收玩家名字) Console.WriteLine("请输入玩家的名字:"); string strName = Console.ReadLine().Trim(); Player player = new Player(strName); //2.创建机器人 Robot robot = new Robot(); //3.创建 裁判 Judger judger = new Judger(); do { //1.接收玩家的输入 FistType typeA = player.Play(); //2.接收机器人的输入 FistType typeB = robot.Play(); //3.将两个输入 传给 裁判,判断并显示结果 judger.Judge(typeA, typeB); //4.是否继续 Console.WriteLine("是否继续?(y/n)"); } while (Console.ReadLine() == "y"); } }}