我必须创建一个模拟电梯运行的程序 [英] I have to create a program that will simulate the operation of an elevator

查看:73
本文介绍了我必须创建一个模拟电梯运行的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的没有代码,我只是有问题,我希望有人可以回答。



1)基本上,一开始,电梯位于给定的楼层。它的第一个动作是加载一些乘客。 //我知道这部分将是一种方法,我只想弄清楚要使用哪些变量。



2)电梯从当前楼层移动到目的楼层:

- 它宣布它通过的每个楼层。 //这个部分会是一个循环吗?根据建筑物的楼层数量而定?

-它不会在目的地楼层的任何一个楼层停下来



3)在目的地楼层,它执行两项操作:

- 卸载一定数量的车手(不一定全部)

- 上载一定数量的人(但不要电梯过载)//这两个动作也会成为装载和卸载车手的循环吗?



我尝试了什么:



我还没试过编码。我的教授说不要看教程,但要试着弄清楚我的代码需要哪些变量和方法。如果你们中的一些人会变得粗鲁,那就不要回答我的问题了。我真的来到这里寻求帮助,因为我的教授或我的同学并不是一个很大的帮助,编程并不容易,所以如果我没有因为试图获得某种帮助而受到攻击,我会非常感激。



无论如何,我需要弄清楚的第一件事是我需要在我的代码中使用哪些变量,有三种类型:变量必须与车手,车手涉及,以及必须与电梯操作的变量。



这里是我到目前为止的变量:

currentFloor
destinationFloor

loadingRiders

unloadingRiders

totalRiders

numberOfFloors

liftUp

elevatorDown



如果有人有任何有用的反馈意见,那将会非常受欢迎,或者他们认为应该添加的任何变量会帮助我很多,这样我知道如何创建我的方法。谢谢。

解决方案

一个有趣的功课,但我不能做你的功课。然而,我太过诱人,我会给你一个良好的开端(作为借口):

让我们来看看车手和升降机之间的相互作用。假设电梯在随机楼层开始,并且乘客从随机楼层呼叫,并且想要骑到不同的随机楼层,则伪码的片段可以表示为:

 numberOfFloors = 10; 
乘客= 0;
elevatorMovement = 0; // -1表示向下,0表示静止,1表示向上

//在程序启动时随机提升
liftCurrentFloor = Math.floor((Math.random()* numberOfFloors)+ 1);

//将电梯移动到主叫乘客的方法
moveLift(destinationFloor){
elevatorMovement = destinationFloor - liftCurrentFloor;
liftCurrentFloor = destinationFloor;

loadPassenger(); //如果乘客在电梯外面

unloadPassenger(); //如果乘客在电梯里面
}

loadPassenger(){
ridership ++;
desiredFloor = Math.floor((Math.random()* numberOfFloors)+ 1);
//在调用
moveLift(desiredFloor)之前使用循环来检查desiredFloor!= liftCurrentFloor;
}

unloadPassenger(){
ridership--;
}

//乘客从随机楼层拨打电梯
callLift(){
moveLift(Math.floor((Math.random()* numberOfFloors )+ 1));
}

我只是投入了一些光线来帮助你开始。实际上,您会期望多名乘客同时从不同楼层呼叫和前往不同楼层。解决这个问题的一种方法是在代码中创建一个乘客对象,其中包括floorAt,floorTo,isInsideLift等属性。在您的电梯对象中,您可以将其作为一组乘客来处理。知道了吗?

快乐的编码!

P.S.至少我已经解渴了一下。


引用:

模拟电梯的运行



电梯是一个自动机,这意味着它可以与事件一起运行。

所以这完全是逻辑问题,所有这些都符合一个问题:如果会发生什么?

要知道你是否需要一个变量,请回答这个问题:它是否会以不同的方式运作?

- totalRiders:它的运行方式是否有所不同骑手的数量?

- loadingRiders,unloadingRiders:加载骑手和卸载骑手的操作方式是否有所不同?

- loadingRiders,unloadingRiders:如果没有骑手,它会有不同的运作吗?装载或卸载。

引用:

1)基本上,在开始时,电梯位于给定楼层。 它的第一个动作是加载一些乘客



您确定第一个动作吗?如果电梯在4楼,你从1楼打电话怎么办。

引用:

3)在目的地地板它执行两个动作:

- 卸载一定数量的车手(不一定全部)

- 上载一定数量的人(但不要超载电梯)//这两个动作也可以循环加载和卸载骑行者吗?



你真的认为电梯正在计算乘客吗?

< blockquote class =quote>

引用:

如果你们中的一些人会变得粗鲁,那就不要回答我的问题了。



有时,让某人对你有点粗鲁,是让你振作起来的最好方法,最后对你好。

Albert Einstein写道:

一切都应尽可能简单,但不能简单。


Hi, I don't really have a code, I just have questions that I'm hoping someone can answer.

1) Basically, at the start, the elevator is situated at a given floor. Its first action is to load on some passengers. // I know this part will be a method, I'm just trying to figure out what variables to use.

2) Elevator moves from current floor to destination floor:
- it announces every floor it passes. // Would this part be a loop? Depending how many floors the building has?
-It will not have to stop at any of these floors just the destination floor

3) At the destination floor it performs two actions:
- offloads a certain amount of riders (not necessarily all of them)
- onload a certain number of people (but do not overload the elevator) // Would these two actions also be loops as well for loading and unloading riders?

What I have tried:

I haven't tried to code anything yet. My professor says not to look at the tutorial, but try and figure out what variables and methods I would need for my code. And if some of you are going to be rude, just don't answer my questions. I genuinely came here for help, because my professor or my classmates aren't really a big help and programming isn't easy so I would appreciate it very much so if I wasn't attacked for trying to get some type of help.

Anyway, the first thing I need to figure out is what variables I need to use in my code, there are three types: variables having to with the riders, the riders involved, and variables having to with the operation of the elevator.

here are the variables I have so far:
currentFloor
destinationFloor
loadingRiders
unloadingRiders
totalRiders
numberOfFloors
elevatorUp
elevatorDown

If anyone has any feedback that would help, that would be greatly apprieciated, or any variables that they think should be added would help me alot, that way I know how to create my methods. Thank you.

解决方案

An interesting homework, but I can't do your homework. Nevertheless, it's too tempting that I will give you a head start (as an excuse):
Let's examine the interactions between a riders and the lift. Assuming the lift starts at a random floor, and a passenger calls it from a random floor, and want to ride to a different random floor, a snippet of the pseudo code can be expressed as shown:

numberOfFloors = 10;
ridership = 0;
elevatorMovement = 0; // -1 for down, 0 for still, 1 for up

// lift at random floor on start of the program
liftCurrentFloor = Math.floor((Math.random() * numberOfFloors) + 1);

// a method to move lift to the calling passenger
moveLift(destinationFloor){
  elevatorMovement = destinationFloor - liftCurrentFloor;
  liftCurrentFloor = destinationFloor;

  loadPassenger(); // If the passenger is outside the lift
   
  unloadPassenger(); // If the passender is inside the lift
}

loadPassenger(){
  ridership++;
  desiredFloor = Math.floor((Math.random() * numberOfFloors) + 1);
  // use a loop to check that the desiredFloor != liftCurrentFloor before calling
  moveLift(desiredFloor);
}

unloadPassenger(){
  ridership--;
}

// A passenger call the lift from a random floor
callLift(){
  moveLift(Math.floor((Math.random() * numberOfFloors) + 1));
}

I have merely thrown in some light to help your kick start. In reality, you will expect multiple passengers calling from and going to different floors simultaneously. One way to tackle this is to create a passenger object in code with properties like floorAt, floorTo, isInsideLift etc. And in your lift object, you can handle them as an array of passengers. Got it?
Happy coding!
P.S. at least I have quenched my thirst a bit.


Quote:

simulate the operation of an elevator


An elevator is an automata, which mean that it operate with events.
So it is all a matter of logic, and all fit in 1 question: "What happens if ?"
To know if you need a variable, answer this question: "Does it operate differently if ?"
- totalRiders: Does it operate differently depending on the number of riders ?
- loadingRiders, unloadingRiders: Does it operate differently for loading riders and for unloading riders ?
- loadingRiders, unloadingRiders: Does it operate differently if no rider load or unload.

Quote:

1) Basically, at the start, the elevator is situated at a given floor. Its first action is to load on some passengers.


Are you sure about the first action ? What if elevator is on floor 4 and you call it from floor 1.

Quote:

3) At the destination floor it performs two actions:
- offloads a certain amount of riders (not necessarily all of them)
- onload a certain number of people (but do not overload the elevator) // Would these two actions also be loops as well for loading and unloading riders?


Do you really think the elevator is counting the passengers ?

Quote:

And if some of you are going to be rude, just don't answer my questions.


Sometimes, having someone being a little rude to you, is the best way to make you crank your brain, and finally being good to you.

Albert Einstein wrote:

Everything should be made as simple as possible, but no simpler.


这篇关于我必须创建一个模拟电梯运行的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆