| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 549 人关注过本帖
标题:哪位高人帮我解答这个题目
只看楼主 加入收藏
统帅堂
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-8-28
收藏
 问题点数:0 回复次数:2 
哪位高人帮我解答这个题目
// A simple elevator simulation

 using System;

 class Elevator
 {
    private int currentFloor = 1;
    private int requestedFloor = 0;
    private int totalFloorsTraveled = 0;
    private Person passenger;

    public void LoadPassenger()
    {
       passenger = new Person();
    }
 
    public void InitiateNewFloorRequest()
    {
       requestedFloor = passenger.NewFloorRequest();
       Console.WriteLine(" Departing floor: " + currentFloor
          + " Traveling to floor: " + requestedFloor);
       totalFloorsTraveled = totalFloorsTraveled +
       Math.Abs(currentFloor - requestedFloor);
       currentFloor = requestedFloor;
    }

    public void ReportStatistic()
    {
       Console.WriteLine("Total floors traveled:"+
          totalFloorsTraveled);
    }
 }

 class Person
 {
    private System.Random randomNumberGenerator;

    public Person()
    {
       randomNumberGenerator = new System.Random();
    }

    public int NewFloorRequest()
    {
        // Return randomly generated number
       return randomNumberGenerator.Next(1,30);
    }
 }

 class Building
 {
     private static Elevator elevatorA;

     public static void Main()
     {
         elevatorA = new Elevator();
         elevatorA.LoadPassenger();
         elevatorA.InitiateNewFloorRequest();
         elevatorA.InitiateNewFloorRequest();
         elevatorA.InitiateNewFloorRequest();
         elevatorA.InitiateNewFloorRequest();
         elevatorA.InitiateNewFloorRequest();
         elevatorA.ReportStatistic();
         Console.Read();
     }
 }
 
 想在这个程序下做如下修改:给Elevator类添加一个表示电梯名的实例变量,可以将他命名为myName,此实例变量是用private还是public?编写一个构造函数,当你用new关键字创建一个Elevator对象并赋予一个变量时,通过构造函数来为此变量设置值,当使用new关键字时,调整对构造函数的调用方法,将Elevator的名字作为一个参数(花括号之间,而不是在new Elevator()之间插入),写成new Elevator("ElevatorA")

Elevator每完成一次运输它必须打印出发和到达楼层名  不是打印:Departing floor: 2 Traveling to floor 24
 应该打印:ElevatorA:Departing floor: 2 Traveling to floor 24 其中ElevatorA是myName中Elevator的名字
搜索更多相关主题的帖子: 高人 解答 
2008-08-28 21:44
统帅堂
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-8-28
收藏
得分:0 
没有哪位高人指点一下吗 急啊!!!
2008-08-28 23:41
jwhandsome521
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2008-9-4
收藏
得分:0 
阿斯顿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Elevator
    {
        private int currentFloor = 1;
        private int requestedFloor = 0;
        private int totalFloorsTraveled = 0;

        private string myName;

        public Elevator(string str)
        {
            myName = str;
        }


        private Person passenger;

        public void LoadPassenger()
        {
            passenger = new Person();
        }

        public void InitiateNewFloorRequest()
        {
            requestedFloor = passenger.NewFloorRequest();
            Console.WriteLine(myName+": Departing floor: " + currentFloor+ " Traveling to floor: " + requestedFloor);
            totalFloorsTraveled = totalFloorsTraveled +Math.Abs(currentFloor - requestedFloor);
            currentFloor = requestedFloor;
        }

        public void ReportStatistic()
        {
            Console.WriteLine("Total floors traveled:" +totalFloorsTraveled);
        }
    }

    class Person
    {
        private System.Random randomNumberGenerator;

        public Person()
        {
            randomNumberGenerator = new System.Random();
        }

        public int NewFloorRequest()
        {
            // Return randomly generated number
            return randomNumberGenerator.Next(1, 30);
        }
    }

    class Building
    {
        private static Elevator elevatorA;

        public static void Main()
        {
            elevatorA = new Elevator("ElevatorA");
            elevatorA.LoadPassenger();
            elevatorA.InitiateNewFloorRequest();
            elevatorA.ReportStatistic();
            //Console.Read();
        }
    }
}
2008-09-04 17:15
快速回复:哪位高人帮我解答这个题目
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022780 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved