我已经在这个电梯程序上工作了很长一段时间了,并最终完成了它并使它工作起来,我为自己感到骄傲,但我想知道如何优化我的代码,以便我可以为未来学习。通过优化,我的意思是使代码看起来更好,也许通过使用更少的代码行。
import java.awt.geom.*;
public class elevator
{
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 19:00:07
import java.awt.geom.*;
public class elevator类名通常是大写的。
{
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();
}而不是将您的选择保留在静态变量中。将其作为参数传递给ElevatorUp()/ElevatorDown()移动函数。
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();
}这是以前重复的。编写一个ElevatorMove()函数,该函数决定是调用ElevatorUp还是ElevatorDown。
}
public static void ElevatorUp()
{
System.out.println("The elevator is on it's way up...");
for (person = choice1; choice1>=floor; floor++)在循环中声明循环变量,而不是其他随机位置。person呢?人和它有什么关系?事实上,person=choice1什么也不做。我会把这做个时间循环。
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");
}这两种功能是相似的。它们可以结合在一起。}
我修改了你的代码:
import java.awt.geom.*;
public class elevator
{
static int floor;
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: ");
int current_floor = Keyboard.readInt();
if(floor == current_floor)
{
System.out.println("Enter the elevator");
}
else
{
MoveElevator(current_floor);
}
System.out.println("To which floor would you want to go (0-10) where 0 = basement");
int target_floor = Keyboard.readInt();
MoveElevator(target_floor);
}
public static void MoveElevator(int target_floor)
{
int direction;
if( target_floor > floor )
{
System.out.println("The elevator is on it's way up...");
direction = 1;
}else{
System.out.println("The elevator is on it's way down...");
direction = -1;
}
while(target_floor != floor)
{
floor += direction;
System.out.println(floor);
}
System.out.println("The elevator has arrived");
}
}发布于 2015-07-17 14:16:46
您应该将main方法从Elevator类中分离出来,这样您的Elevator类才能真正代表电梯!比方说,您可以拥有一个Program类,它将调用您的Elevator类。
其次,您的方法和字段不应该是静态的。通常,当您的类具有状态(例如当前地板)时,您的对象不应该是静态的。这样,您就可以拥有2个(或更多) Elevator实例,并且它们不会碰撞在一起。
电梯不应该从一个随机的楼层开始,想象一下真正的电梯。在第一次使用时,它很有可能从底层开始(至少,我想,我不是电梯专家!)让我们在0楼(地下室)启动这台电梯。
所以现在,我假设你的电梯只有一个人使用,因为一个真正的电梯可能不会来接你,如果它没有朝你想要的方向走。
类名应该是大写的,而命名的方法应该是camelCased,因此:
Elevator而不是elevator
elevatorUp而不是elevatorUp
elevatorDown而不是elevatorDown
而且,Java约定指定您的括号应该是“埃及风格”。这意味着:
//Good
if{
}
//Less good
if
{
}这是我解释的简历:
public class Elevator {
private int floor = 0, choice1, person = 0;
public void callFrom(int floor){
System.out.println("Elevator is coming");
goTo(floor);
}
public void goTo(int floor){
if(floor == choice1) {
System.out.println("Enter the elevator");
}
else if(floor > choice1) {
elevatorDown();
}
else if(floor < choice1){
elevatorUp();
}
}
private 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");
}
private 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");
}
}我不确定goTo和callFrom,但我认为把它们分开很重要,因为如果你想让电梯变得更加复杂(处理多个电话等等)你得把他们分开。
发布于 2012-03-29 00:21:06
在面向对象编程中,您通常会问自己:我使用哪些对象操作,我可以对它们做什么,以及它们的属性是什么?就你而言:
下面是基于上述描述的代码:
public class Elevator {
private int currentFloor;
public void run() {
currentFloor = 1 + ((int) (Math.random() * 10));
System.out.println("The elevator is now on floor " + currentFloor);
System.out.print("Which floor are you at now (0-10) where 0 = basement: ");
int personFloor = Keyboard.readInt();
moveTo(personFloor);
System.out.println("Enter the elevator");
System.out.println("To which floor do you want to go (0-10) where 0 = basement");
int destinationFloor = Keyboard.readInt();
moveTo(destinationFloor);
System.out.println("Leave the elevator");
}
private void moveTo(int destinationFloor) {
if (destinationFloor == currentFloor) {
/* nothing to do */
} else if (destinationFloor > currentFloor) {
moveUpTo(destinationFloor);
} else {
moveDownTo(destinationFloor);
}
}
private void moveUpTo(int destinationFloor) {
System.out.println("The elevator is on its way up ...");
while (currentFloor < destinationFloor) {
currentFloor++;
System.out.println(currentFloor);
}
System.out.println("The elevator has arrived");
}
private void moveDownTo(int destinationFloor) {
System.out.println("The elevator is on its way down ...");
while (currentFloor > destinationFloor) {
currentFloor--;
System.out.println(currentFloor);
}
System.out.println("The elevator has arrived");
}
public static void main(String[] args) {
new Elevator().run();
}
}顺便说一下,电梯在路上,而不是在路上。
https://codereview.stackexchange.com/questions/7990
复制相似问题