gpt4 book ai didi

Java新手-汽车应用

转载 作者:行者123 更新时间:2023-12-01 07:35:26 25 4
gpt4 key购买 nike

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

我得到的指导方针:创建一个由两个类组成的 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

抱歉,如果这篇文章的内容很奇怪。我输入得很好,但预览看起来很奇怪。

最佳答案

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

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

更正了这一点

关于Java新手-汽车应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12683114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com