布尔变量在应该更改时没有改变。

huangapple go评论67阅读模式
英文:

Boolean variable not changing when it should

问题

我正在完成一项作业任务演示了Java中面向对象编程OOP的基础知识这是一个非常简单的汽车洗车模拟项目`Car`类中我有两个方法,`isDirty``getDirty`。在汽车类中我有一个名为`Dirty`的布尔变量初始设置为`false`,因为所有3辆汽车都应该是干净的我需要使用`isDirty`方法检查这3辆汽车以证明它们初始状态都是干净的之后我需要运行`getDirty`方法使汽车变脏然后再次运行`isDirty`方法以展示状态从干净变为脏但是由于某种原因在运行了`getDirty`方法之后,`boolean`变量`Dirty`并没有改变为true

```java
public class CarwashSimulation
{
  Carwash suds = new Carwash();
  Car carA = new Car();
  Car carB = new Car();
  Car carC = new Car();

  public void runSimulation()
  {
    System.out.println("开始模拟");
    carA.isDirty();
    carB.isDirty();
    carC.isDirty();
    carA.getDirty();
    carB.getDirty();
    carC.getDirty();
    carA.isDirty();
    carB.isDirty();
    carC.isDirty();
  }
}

class Carwash
{
  public void washCar()
  {
    
  }
}

class Car
{
  boolean Dirty = false;

  public void getDirty()
  {
    Dirty = true;
  }

  public boolean isDirty()
  {
    if (Dirty == true)
    {
      System.out.println("它是脏的");
      return true;
    }
    else
    {
      System.out.println("它是干净的");
      return false;
    }
  }
}

<details>
<summary>英文:</summary>
I&#39;m currently working on a homework assignment that illustrates the basics of OOP in Java. It&#39;s a very simple carwash simulation. I have two methods in the `Car` class, `isDirty` and `getDirty`. I have a `boolean` variable `Dirty` in the car class which is set to `false` because all 3 cars are supposed to start out clean. I&#39;m supposed to check all 3 cars with the `isDirty` method to show that all 3 cars start out clean, after, I&#39;m supposed to run the `getDirty` method to make the cars dirty and then run the `isDirty` method to show that the status changed from clean to dirty. For some reason the `boolean` variable `Dirty` isn&#39;t changing to true after running the `getDirty` method.

public class CarwashSimulation
{
Carwash suds = new Carwash();
Car carA = new Car();
Car carB = new Car();
Car carC = new Car();

public void runSimulation()
{
System.out.println("Start Simulation");
carA.isDirty();
carB.isDirty();
carC.isDirty();
carA.getDirty();
carB.getDirty();
carC.getDirty();
carA.isDirty();
carB.isDirty();
carC.isDirty();
}
}

class Carwash
{
public void washCar()
{

}

}

class Car
{
boolean Dirty = false;

public void getDirty()
{
boolean Dirty = true;
}

public boolean isDirty()
{
if (Dirty == true)
{
System.out.println("It's Dirty");
return true;
}
else
{
System.out.println("It's Clean");
return false;
}
}
}


</details>
# 答案1
**得分**: 3
在方法 `getDirty()` 中,创建了一个新变量 `boolean Dirty` 并赋值为 `true`。然而,我们想要的是将(已存在的)字段 `Dirty` 设置为 `true`。为了实现这一点,我们只需要移除 `boolean`:
```java
class Car {
...
public void getDirty() {
Dirty = true; 
}
...
}

关于代码的一些建议:

  • 在Java中,字段名应始终以小写字母开头(Dirty -> dirty
  • 方法名称中的前缀 get 通常用于表示获取方法(getter)。我建议将 getDirty() 重命名为类似 makeDirty() 的名称,以免用户将其误认为是一个getter方法。
英文:

In method getDirty(), a new variable boolean Dirty is created and assigned the value true. However, what we want is to set the (existing) field Dirty to true. In order to achieve this, we only need to remove the boolean:

class Car {
...
public void getDirty() {
Dirty = true; 
}
...
}

Some remarks on the code:

  • In java, field names should always start with a lowercase letter (Dirty -> dirty)
  • The prefix get in method names is normally used for getters. I would suggest to rename getDirty() to something like makeDirty() so that a user does not confuse it for a getter.

答案2

得分: 2

getDirty()没有更新数据成员,而是声明了一个本地布尔变量,隐藏了数据成员,将其初始化为true,然后在作用域结束时丢弃它。移除该声明,你应该没问题:

public void getDirty()
{
    Dirty = true; // 这里!
}

附注:
如果该方法旨在设置脏状态,setDirty 可能是一个更好的名称。

英文:

getDirty() is not updating the data member, but declaring a local boolean that hides the data member, initializes it to true and then discards it when it goes out of scope. Remove the declaration, and you should be OK:

public void getDirty()
{
Dirty = true; // Here!
}

Side note:<br/>
If this method is supposed to set the dirty state, setDirty is probably a better name for it.

huangapple
  • 本文由 发表于 2020年9月22日 19:27:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64008770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定