繁体   English   中英

Java新手-汽车应用

[英]Java newbie - car application

我是学生,是的,这是我的作业。 我花了上周的时间来阅读笔记,阅读本书并研究网上的相关主题,但是我只是不明白问题出在哪里。 你能告诉我我做错了吗? 任何帮助将不胜感激。 (我仅使用记事本和命令提示符。)

我得到的指导方针是:创建一个由两个类组成的Java应用程序。 第一类将是您的应用程序类。 第二堂课将称为汽车课。 您的应用程序将创建一个名为nova的Car实例,并对其进行驱动。

汽车规则:

  • 如果汽车没有启动,则无法驾驶(向控制台发送错误消息)。
  • 如果汽车没有启动,则无法停止(向控制台发送错误消息)。
  • 如果汽车已经启动,则无法启动(将错误消息发送到控制台)。
  • 告诉汽车驾驶之后,您唯一可以做的就是停车(向控制台发送消息)
  • 一旦您叫停,汽车将返回初始状态,用户必须先启动汽车,然后才能尝试执行其他任何功能。 (向控制台发送消息)

showState方法的目的是提供一种检查汽车状态的方法。 它应该生成一条消息,然后可以将其发送到控制台。

我的代码:

public class MyAppAssignment3
{
   public static void main (String[] args)
   {
       System.out.println("Scenario 1");
       Car nova1 = new Car();
       nova1.start();
       nova1.showState();
       nova1.drive();
       nova1.stop();
       nova1.showState();
       System.out.println("");

       System.out.println("Scenario 2");
       Car nova2 = new Car();
       nova2.showState();
       nova2.drive();  //needs to send error message - can't drive a car that's not started
       nova2.stop();
       nova2.showState();
       System.out.println("");

       System.out.println("Scenario 3");
       Car nova3 = new Car();
       nova3.showState();
       nova3.start();
       nova3.showState();
       nova3.stop();   //needs to send error message - can't stop a car that's not driving
       nova3.showState();
       nova3.drive();
       nova3.stop();
   }
}

class Car
{
   private boolean isStarted;
   private boolean isDriving;
   private boolean isStopped;
   private String showState;

   public Car()
   {
       this.showState = showState;
   }

   public void start()
   {
       isStarted = true;
       isDriving = false;
       isStopped = false;
       System.out.println("The car is " + this.showState);
   }

   public void drive()
   {
       isStarted = false;
       isStopped = false;
       isDriving = true;
       System.out.println("The car is " + this.showState);
   }

   public void stop()
   {
       isStopped = true;
       isStarted = false;
       isDriving = false;
       System.out.println("The car is " + this.showState);
   }

   public String showState()
   {
       if (isStarted)
       {
           showState = "started";
       }
       else if(isDriving)
       {
           showState = "driving";
       }
       else if(isStopped)
       {
           showState = "stopped";
       }
       System.out.println("The car is " + this.showState);
       return showState;
   }
}

我的输出(全部错误-值不正确):

Scenario 1
The car is null
The car is started
The car is started
The car is started
The car is stopped

Scenario 2
The car is null
The car is null
The car is null
The car is stopped

Scenario 3
The car is null
The car is null
The car is started
The car is started
The car is stopped
The car is stopped
The car is stopped

对不起,如果这一切都发呆。 我输入的很好,但是预览看起来很麻烦。

这实际上没有做任何事情...

public Car()
{
    this.showState = showState;
}

基本上,它只是将相同的值重新分配给自己。 我将更改为初始状态,可能已stopped

我会用enum来表示自己的汽车状态,而不是依靠boolean状态,这可能会变得混乱不堪。

public enum CarState {
    Stopped,
    Started,
    Driving
}

然后只需将其分配给单个state变量...

class Car
{
    private CarState state;

    public Car()
    {
       this.state= CarState.Stopped;
    }

    public void start()
    {
        if (state.equals(State.Stopped)) {
            state = CarState.Started;
            showState();
        } else {
            System.error.println("Car is not in a valid state to be started");
        }
    }

    public void drive()
    {
        if (state.equals(State.Started)) {
            state = CarState.Driving;
            showState();
        } else {
            System.error.println("Car is not in a valid state to be driven");
        }
    }

    public void stop()
    {
        if (state.equals(State.Driving)) {
            state = CarState.Stopped;
            showState();
        } else {
            System.error.println("Car is not in a valid state to be stopped");
        }
    }

    public String showState()
    {
        System.out.println("The car is " + state);
    }
}

您遇到的另一个问题是,更改状态时未调用showStatus ,即未将当前状态分配给showState变量...我已使用enum showState进行了更正

对于一个,您只是在创建一个新实例。 您从未真正为这些实例设置默认值。

至少考虑这样的事情:

public Car()
{
    isStopped = true;
}

这样,当您调用第一个nova1.start(); 您可以先检查isStopped是否为true,然后再重新启动...

public void start()
{
    if(isStopped)
    {
        isStarted = true;       
        isDriving = false;       
        isStopped = false;   
        showState = "started";    
        System.out.println("The car is " + this.showState);
    }
}

只是一个例子。 但是您可以轻松地使用它来推断其余需求。 我的观点主要是创建一个实例,然后期望布尔值具有未指定的值。 您可以在默认值或构造函数中执行此操作。

例如:

private boolean isStarted = false;

使用枚举是一个好主意。 这是使用Enum的实现,使用默认实现的Enum和使用类型系统的自己的实现。 也没有条件,例如if或switch。 纯粹而美丽的Java代码。

public class Car {
private enum State {
  OFF {
    void start(Car c) {
      System.out.println("Starting the car");
      c.state = State.STARTED;
    }
  }, 
  STARTED {
    void stop(Car c) {
      System.out.println("Stopping the car");
      c.state = State.OFF;
    }
    void drive(Car c) {
      System.out.println("Driving the car");
      c.state = State.DRIVING;
    }
  }, 
  DRIVING {
    void stop (Car c) {
      System.out.println("Stopping the car");
      c.state = State.OFF;
    }
  };

  void start(Car c) {
    System.err.println("Can't start");
  }

  void stop(Car c) {
    System.err.println("Can't stop");
  }

  void drive(Car c) {
    System.err.println("Can't drive");
  }
}
  private State state = State.OFF;

  public void start(){
    state.start(this);
  }

  public void stop(){
    state.stop(this);
  }
  public void drive() {
    state.drive(this);
  }

  public void showState(){
    System.out.println("The car is "+state);
  }
}

我的建议是,每辆车都有自己的唯一ID:

 class Car
{
   private boolean isStarted;
   private boolean isDriving;
   private boolean isStopped;
   private String showState;
   private int id;

   public Car(Integer id)
   {
       this.id = id;
   }
...

}

那么在您说要打印的所有地方,还应包含ID:

System.out.println("The car id "+id+" is "+ this.showState);

然后创建一个像这样的对象:

Car nova1 = new Car(1);

Car nova2 = new Car(2);

Car nova3 = new Car(3);

它不是解决方案,但却为解决方案提供了方法。 您会找到解决方案,并且您会觉得有味道

您必须将所需的内容翻译成代码,并且您可以看到它甚至接近实际需求-例如:

如果汽车没有启动 则无法驾驶(向控制台发送错误消息)。

变成:

public void drive()
{

    if( this.isStarted == false ){

        System.out.println("You should start the car first!");

    }else{

        System.out.println("Car is running!");

    }

}

请注意,您可以将!this.isStarted编写为isStarted == false的简写。

尝试在每个步骤中输出变量的值。 逻辑流程中存在一些问题。 例如,检查构造函数。

public Car()
{
   System.out.println(showState);
   this.showState = showState;
}

没有将showState值传递给构造函数,也没有在函数内部对其进行初始化。

另外,在每个函数的启动,停止和驱动中,您需要编写:

  System.out.println("The car is " + this.showState());

代替 :

  System.out.println("The car is " + this.showState);

让我们保持简单,为什么只需要两个变量就使用三个变量? 如果我错了,请更正,但是如果没有启动汽车并且您没有驾驶汽车,那么它将停止,对吗? 看我的课:

public class car
{
private boolean isStarted;
private boolean isDriving;

public car()
{
    isStarted = false;
    isDriving = false;
    //Initial State
    showState();
}

public void start()
{
    if(!isStarted)
    {
        if(!isDriving)
            isStarted = true;
    }
    else
        System.err.println("You can\'t start a car which is already started"); //You can’t start a car if it is already started (send an error message to the console).
        showState();
}   

public void drive()
{
    if(isStarted)
        isDriving = true;
    else
        System.err.println("You can\'t drive a car which is not started");

    showState();
}

public void stop()
{
    if(isStarted)
    {
        isStarted = false;
        isDriving = false;
        // Once you call stop, the car will return to the initial state and the user must start the car before attempting to do any other functions. (Send a message to the console. (Below on ShowState)
    }
    else
        System.err.println("You can\'t stop a car which is not started"); // You can’t stop a car if it is not started (send an error message to the console).
    showState(); // Once you tell the car to drive, the only thing you can do is stop (Send a message to the console)
}

public void showState()
{
    if(isStarted && isDriving)
        System.out.println("It\'s Driving");
    if(!isStarted && !isDriving)
        System.out.println("It\'s Stopped");
    if(isStarted && !isDriving)
        System.out.println("It\'s Started");
}

}

我希望它能有所帮助。 干杯

这工作了! 感谢您的所有帮助!

public class MyAppAssignment3
{
    public static void main (String[] args)
    {
        System.out.println("Scenario 1");
        Car nova1 = new Car();
        nova1.start();
        nova1.showState();
        nova1.drive();
        nova1.stop();   
        nova1.showState(); 
        System.out.println("");

        System.out.println("Scenario 2");
        Car nova2 = new Car();
        nova2.showState();
        nova2.drive();
        nova2.stop();
        nova2.showState();
        System.out.println("");

        System.out.println("Scenario 3");
        Car nova3 = new Car();
        nova3.showState();
        nova3.start();
        nova3.showState();
        nova3.stop();
        nova3.showState();
        nova3.drive();
        nova3.stop();
    }
}

class Car
{
    private boolean isStarted;
    private boolean isDriving;
    private boolean isStopped;
    private String showState;

    public Car()
    {
        isStarted = false;
        isDriving = false;
        isStopped = true;
    }

    public void start()
    {
        if(isStarted == false)
        {
            isStopped = false;
            isStarted = true;
            showState();
        }
        else
        {
            System.out.println("You can't start a car which is already started.");
        }

    }

    public void drive()
    {
        if(isStarted)
        {
            isDriving = true;
            showState();
        }
        else
        {
            System.out.println("You can't drive a car which is not started.");
        }

    }

    public void stop()
    {
        if(isStarted)
        {
            isStarted = false;
            isDriving = false;
            isStopped = true;
            showState();
        }
        else
        {
            System.out.println("You can't stop a car which is not started.");
        }

    }

    public String showState()
    {
        if(isStarted && (isDriving == false))
        {
            showState = "started";
        }
        else if(isStarted && isDriving)
        {
            showState = "driving";
        }
        else if(isStopped)
        {
            showState = "stopped";
        }
        System.out.println("The car is " + this.showState + ".");
        return showState;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM