A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 管冉 中级黑马   /  2012-12-24 18:22  /  1931 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 管冉 于 2012-12-24 20:34 编辑

写一个Ticket类,有一个距离属性(本属性只读,在构造方法中赋值),不能为负数,有一个价格属性,价格属性只读,并且根据距离计算价格(1元/公里):

0-100公里        票价不打折

101-200公里    总额打9.5折

201-300公里    总额打9折

300公里以上    总额打8折

有一个方法,可以显示这张票的信息.

本来在论坛里找到个一模一样Ticket类的,可是没有写主函数的调用,自己弄了半天,也没把调用方法写好,基础测试啊,这题整整困扰了我1个多小时

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

5 个回复

倒序浏览
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace tickt
{
    class Program
    {
        static void Main(string[] args)
        {
            tickt s = new tickt(100.0);
            Console.WriteLine(s.Money );
            Console.ReadKey();

        }
    }
}这是program类


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

namespace tickt
{
    class tickt
    {
        private double  distance;

        public double  Distance
        {
            get { return distance; }
            
        }
        private double  money;

        public double  Money
        {
            get { return money; }
            
        }
        public tickt(double  distance)
        {
            if (distance  < 0)
            {
                Console.WriteLine("请输入正确的");

            }
            else if (distance < 101)
            {
              this .distance = distance;
                this .money =distance ;
              
            }
            else if (distance > 101 && distance < 201)
            {
                this.distance = distance;
                this.money = 0.95 * distance;
            }
            else if (distance > 201 && distance < 301)
            {
                this.distance = distance;
                this.money = distance * 0.9;
            }
            else
            {
                this.distance = distance;
                this.money = 0.8 * distance;
            }



        }
      
    }
}
这是tickt类

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
   class Ticket
    {
        int distance;
        public int Distance
        {
            get
            {
                if (this.distance >= 0)
                {
                    return this.distance;
                }
                else
                { return 0; }
            }
        }
        double price;
        public double Price
        {
            get
            {
                if (this.distance <= 100)
                {
                    this.price = this.distance;
                    return price;
                }
                else if (this.distance > 100 && this.distance <= 200)
                {
                    this.price = this.distance * 0.95;
                    return this.price;
                }
                else if (this.distance > 200 && this.distance <= 300)
                {
                    this.price = this.distance * 0.9;
                    return this.price;
                }
                else if (this.distance > 300)
                {
                    this.price = this.distance * 0.8;
                    return this.price;
                }
                else
                {
                    return 0;
                }
            }
        }
        public void Show()
        {
            Console.WriteLine("路程"+Distance+"里,价格+"+Price);
        }

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
主函数这么简单啊!看样子我又得把面向对象基础的视频拿出来看看了 对类,方法什么了调用起来总犯迷糊
回复 使用道具 举报
  1.     class Ticket
  2.     {
  3.         /// <summary>
  4.         /// Ticket类构造函数()
  5.         /// </summary>
  6.         /// <param name="d">距离</param>
  7.         public Ticket(int d)
  8.         {
  9.             if (d >= 0)
  10.             {
  11.                 this.distance = d;
  12.             }
  13.             else
  14.             {
  15.                 Console.WriteLine("距离不能为负数");
  16.                 return;
  17.             }
  18.         }
  19.         int distance;
  20.         public int Distance
  21.         {
  22.             get { return distance; }
  23.         }
  24.         decimal price;
  25.         public decimal Price
  26.         {
  27.             get
  28.             {
  29.                 price = distance;
  30.                 if (100 < distance && distance < 201)
  31.                 {
  32.                     price = price * 95 / 100;
  33.                 }
  34.                 else if (200 < distance && distance < 301)
  35.                 {
  36.                     price = price * 90 / 100;
  37.                 }
  38.                 else if (300 < distance)
  39.                 {
  40.                     price = price * 85 / 100;
  41.                 }
  42.                 return price;
  43.             }
  44.         }
  45.     }
复制代码

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 赵小江 于 2012-12-25 03:28 编辑
  1. public class Ticket
  2. {
  3. private int distance;
  4. private double price=1;

  5. public Ticket(int distancd) //这是构造函数
  6. { this.distance = distancd; }

  7. public int Distancd //这是距离属性
  8. {
  9. get { return distance; }
  10. }
  11. public double Price //这是价格属性
  12. {
  13. get { return price; }
  14. }

  15. public void info() //打不打折在方法里判断
  16. {
  17. if (distance <= 100)
  18. { Console.WriteLine("0-100公里 票价不打折.票价为:{0}", distance * price); }
  19. else if (distance > 100 && distance < 200)
  20. { Console.WriteLine("101-200公里 总额打9.5折.票价为:{0}", distance * price * 0.9); }
  21. }

  22. }
复制代码
这样写简单些.

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马