0

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

我得到的指导方针:创建一个包含两个类的 Java 应用程序。第一类将是您的应用程序类。第二类将是一个名为 Car 的类。您的应用程序将创建一个名为 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

抱歉,如果这发布的所有内容都不稳定。我打字很好,但预览看起来很糟糕。

4

8 回答 8

4

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

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

于 2012-10-02T01:03:58.600 回答
2

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

至少考虑这样的事情:

public Car()
{
    isStopped = true;
}

这样,当你第一次打电话时,nova1.start();你可以在允许它重新开始之前检查 isStopped 是否为真......

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;
于 2012-10-02T00:56:18.227 回答
1

使用枚举是个好主意。这是一个使用枚举的实现,使用默认实现的枚举和使用类型系统的自己的实现。也没有任何条件,例如使用 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);
  }
}
于 2012-10-02T01:41:02.103 回答
1

这行得通!感谢所有的帮助!

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;
    }

}
于 2012-10-02T20:37:33.423 回答
0

我的建议是,每辆车都有自己唯一的 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);

它不是解决方案,但它为解决方案提供了途径。你会找到解决方案,你会感受到它的味道

于 2012-10-02T00:52:54.297 回答
0

您必须在代码中翻译您被要求的内容,并且您可以看到它甚至接近实际要求 - 例如:

如果汽车未启动,您将无法驾驶(向控制台发送错误消息)。

变成:

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.

于 2012-10-02T01:01:00.200 回答
0

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

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);
于 2012-10-02T01:10:51.837 回答
0

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

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");
}

}

我希望它有所帮助。干杯

于 2012-10-02T01:21:07.183 回答