【问题标题】:Why does the counter inside if statement not work?为什么if语句里面的计数器不起作用?
【发布时间】:2021-08-02 05:59:11
【问题描述】:

朋友们好,我正在尝试为一个项目构建一个 Car 类。以下代码中有许多方法以及我无法构建的 if 语句,请考虑以下代码

public class Car extends Vehicle {
    private boolean isDriving;
    private final int horsepower;
    private boolean needsMaintenance = false;
    private int tripsSinceMaintenance = 0;

    Car() {
        super();
        this.horsepower = 0;
        this.isDriving = false;
        this.needsMaintenance = false;
        this.tripsSinceMaintenance = 0;
    }

    public int getHorsepower() {
        return this.horsepower;
    }

    public boolean getDrive() {
        return this.isDriving;
    }

    public boolean getMain() {
        return this.needsMaintenance;
    }

    public int getTRIP() {
        return this.tripsSinceMaintenance;
    }

    public void drive() {
        this.isDriving = true;
    }

    public void stop() {
        this.isDriving = false;
    }


    public void repair() {
        this.needsMaintenance = false;
        this.tripsSinceMaintenance = 0;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        Car auto = new Car();
        auto.drive();
        auto.stop();

        if (auto.isDriving == true) {
            if (auto.isDriving == false)
                auto.tripsSinceMaintenance = auto.tripsSinceMaintenance + 1;
        }

        if (auto.tripsSinceMaintenance > 100)
            auto.needsMaintenance = true;
        System.out.println("Drive: " + auto.getDrive());
        System.out.println("trip: " + auto.getTRIP());
    }
}

我想要做的是每当属性isDrivingtrue 变为falsetripsSinceMaintenance should increase by 1 and also when tripsSinceMaintenanceis greater than 100,needsMaintenanceshould becometrue`。

这里我预计trips 是1,但结果如下:

驱动器:假 行程:0

我已经尝试过this.isDriving==true;,并且基本上auto 在我放入 this 的 if 语句中,但出现以下错误

不能从静态上下文中引用非静态变量

请帮帮我!

【问题讨论】:

  • 您有两个嵌套的if 语句。仔细检查每个语句的条件以及它们如何相互作用。
  • auto.isDriving 不能同时为真和假
  • @OldProgrammer 你是完全正确的谢谢!!!!

标签: java if-statement counter


【解决方案1】:

我想要做的是每当属性 isDriving 从 true 变为 false 时,tripsSinceMaintenance 应该增加 1,并且当 tripsSinceMaintenance 大于 100 时,needsMaintenance 应该变为 true

在 stop() 方法中执行此操作

fun stop() {
    if (isDriving) {
        tripsSinceMaintenance++;
    }
    if (tripsSinceMaintenance > 100) {
        needsMaintenance = true;
    }
    isDriving = false;
}

【讨论】:

    【解决方案2】:

    您不需要将 == true 放在 if 语句中,它已经这样做了,

    if(someCondition) { // <-- this executes if the condition is true.
    

    另外,你有嵌套的冲突条件,这意味着......

    if (thisIsTrue) {
        if (!thisIsTrue) { 
            // <--- unreachable statements
    

    你应该在哪里增加你的变量是你设置“isDriving = true”的地方 所以你的代码看起来像这样:

    public void drive() {
        this.isDriving=true;
        auto.tripsSinceMaintenance++;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      相关资源
      最近更新 更多