Java变量只接受特定范围的值

我对此很陌生,所以请耐心等待。我有作业,我不太明白老师这句话到底是什么意思。

班级名称是电梯

level 存储当前级别。level 是 int 类型的实例变量

将级别数限制在 -2 到 10 之间。

然后创建一个名为 goTo(Level: int) 的方法。(将发送消息)

通过限制到特定范围,我猜他的意思是使用 if 语句?如果我是对的,我会将其添加到下面的方法中,因为我无法直接在类中创建 if 语句。

还有为什么方法 where(): int 应该返回 level。它从来没有被使用过...

public class Elevator {


    int level = 0;


    public void goTo(int level){


        if (level < this.level){

            System.out.println("Elevator going up to floor " + level);

        }

        else if (level > this.level){

            System.out.println("Elevator going  down to floor " + level);

        }

        else if (level == this.level){

            System.out.println("Elevator already on the floor " + level);

        }


    }


    public int where(){

        return level;

    }

}


POPMUISE
浏览 130回答 3
3回答

慕码人8056858

从我的角度来看, limit 应该是 goTo 方法中的 if 语句,以防止发送级别不同于 -2 到 10 的级别。所以它应该看起来像这样:public class Elevator {&nbsp; &nbsp; int actualLevel = 0;&nbsp; &nbsp; public void goTo(int level) {&nbsp; &nbsp; &nbsp; &nbsp; if (level < -2 || level > 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid level!. Levels range is -2 to 10.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (level > actualLevel) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Elevator going up to floor " + level);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.actualLevel = level;&nbsp; &nbsp; &nbsp; &nbsp; } else if (level < actualLevel) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Elevator going&nbsp; down to floor " + level);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.actualLevel = level;&nbsp; &nbsp; &nbsp; &nbsp; } else if (level == actualLevel) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Elevator already on the floor " + level);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public int where() {&nbsp; &nbsp; &nbsp; &nbsp; return this.actualLevel;&nbsp; &nbsp; }}另外,我对您的类变量名称进行了更改,因为在检查电梯是否上升或下降时出现了错误,因为您弄错了方法和类变量。另一件事是,如果电梯上升或下降,您应该将其分配给您的类变量以保存实际楼层。

慕哥9229398

一点补充:将级别数限制在 -2 到 10 之间。这里真正的问题不是您需要一系列 if/else 语句。它实际上是(被遗漏的)细节:如何对错误输入做出反应。在您的情况下,您可能会同意:当提供错误的数字时,该方法什么也不goTo()做。然而,在更复杂的情况下,以某种方式告诉该方法的调用者可能更合适:您给我的输入无效。例如通过抛出某种排序异常。回到这个简单的情况,您的代码中缺少的是:当用户选择有效的新级别时,您希望发生该分配:if(newLevel < - 2 || newLevel > 10 ) {... // checks for valid inputif (newLevel < newLevel) {... // the required print statements// finally, make the elevator actually go to the new levellevel = newLevel;(是的:更喜欢以与类字段不同的方式命名该参数。参数实际上是:要移动到的预期新级别。不同的名称有助于避免错误)

守着星空守着你

level首先,本地层和电梯层之间存在一些小小的混淆。现在最小和最大级别是电梯的属性。您也可以将它们存储为(常量)字段。下面我直接在代码中使用它们。这并不容易理解,房子只有这些楼层。public class Elevator {&nbsp; &nbsp; int cabinLevel = 0;&nbsp; &nbsp; public void goTo(int level) {&nbsp; &nbsp; &nbsp; &nbsp; if (level < -2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (level Y > 12) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (level < this.cabinLevel){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Elevator going D O W N to floor " + level);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cabinLevel = level;&nbsp; &nbsp; &nbsp; &nbsp; } else if (...) {
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java