我已经在这个电梯程序上工作了很长一段时间,最终完成了它,并使它工作,我为自己感到非常自豪,但我想看看如何优化我的代码,以便我可以学习未来。通过优化,我的意思是,让代码看起来更好,也许是通过使用更少的代码行。
static int floor = 0, choice1, person = 0;
public static void main(String args[])
{
floor = ((int) (Math.random() * 10 + 1));
System.out.println("The elevator is now on floor " +floor);
System.out.print("Which floor are you at now (0-10) where 0 = basement: ");
choice1 = Keyboard.readInt();
if(floor == choice1)
{
System.out.println("Enter the elevator");
}
else if(floor > choice1)
{
ElevatorDown();
}
else if(floor < choice1)
{
ElevatorUp();
}
System.out.println("To which floor would you want to go (0-10) where 0 = basement");
choice1 = Keyboard.readInt();
if(floor > choice1)
{
ElevatorDown();
}
else if(floor < choice1)
{
ElevatorUp();
}
}
public static void ElevatorUp()
{
System.out.println("The elevator is on it's way up...");
for (person = choice1; choice1>=floor; floor++)
System.out.println(floor);
System.out.println("The elevator has arrived");
}
public static void ElevatorDown()
{
System.out.println("The elevator is on it's way down...");
for (person = choice1; choice1<=floor; floor--)
System.out.println(floor);
System.out.println("The elevator has arrived");
}发布于 2012-01-20 18:05:46
试着让你的代码成为object-oriented.将电梯建模为对象。电梯能做什么?它可以上升和下降,所以你需要几个方法来做到这一点。电梯有哪些属性?它有一个当前的floor,这将是一个实例变量。您还需要一个构造函数来创建您的电梯。
确保使用有意义的变量名称,并适当地注释代码。
这里有一些代码可以帮助你:
public class Elevator {
// the floor that the elevator is currently on
private int currentFloor;
/**
* Creates an elevator at the specified floor.
*
* @param initialFloor the initial floor
*/
public Elevator(int initialFloor) {
this.currentFloor = initialFloor;
}
/**
* @return the currentFloor
*/
public int getCurrentFloor() {
return currentFloor;
}
/**
* Moves the elevator to the specified floor.
*
* @param floor the floor to go to.
*/
public void goToFloor(int floor) {
if (floor < currentFloor) {
goDownToFloor(floor);
} else if (floor > currentFloor) {
goUpToFloor(floor);
}
System.out.println("The elevator has arrived");
}
/**
* Moves the elevator up to the specified floor.
*
* @param floor the floor to go up to.
*/
private void goUpToFloor(int floor) {
System.out.println("The elevator is on its way up...");
//TODO: put loop to go up to the floor here
}
/**
* Moves the elevator down to the specified floor
*
* @param floor the floor to go down to.
*/
private void goDownToFloor(int floor) {
System.out.println("The elevator is on its way down...");
//TODO: put loop to go down to the floor here
}
}现在您需要一个main方法来创建电梯,读取用户输入并控制它。您可以为此创建一个新类,也可以将其添加到上面的Elevator类中。
public static void main(String[] args) throws Exception {
//create an elevator at a random floor
Elevator elevator = new Elevator(new Random().nextInt(11));
int elevatorFloor = elevator.getCurrentFloor();
System.out.println("The elevator is now on floor " + elevatorFloor);
System.out.print("Which floor are you at now? (0-10) where 0 = basement: ");
int personFloor = Keyboard.readInt();
if(personFloor == elevatorFloor) {
System.out.println("Enter the elevator");
}
else {
elevator.goToFloor(personFloor);
}
System.out.println("To which floor would you want to go (0-10) where 0 = basement");
int destinationFloor = Keyboard.readInt();
elevator.goToFloor(destinationFloor);
}将此方法与您当前的方法进行比较。它根据您可以对其执行的对象和操作对问题进行了更好的建模。代码重复的情况也更少。
发布于 2012-01-20 17:39:16
发布于 2012-01-20 17:45:51
你可以在你的代码中做很多事情...除了Tichodroma提到的所有优点之外,你还需要在你的代码中进行异常处理。
我要消除的另一件事是doble代码部分,比如:
if(floor > choice1)
{
ElevatorDown();
}这些部分必须被提取出来,这样它才能只写一次。双代码段通常是code smell的指示器。
所有输出节(System.out.println...)可以在一种输出例程中提取,在我看来这样会更好。
https://stackoverflow.com/questions/8939299
复制相似问题