如何设置部门中的属性在特定数值范围内?

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

How to set an attributes in departments to be between certain numbers?

问题

I'm trying to set the attribute "Hour" to be between the numbers 0 and 23, How?
我想将属性“Hour”设置为介于0和23之间的数字,如何实现?

I'm trying to do it with a set function. So any answers to HOW?
我想使用一个设置函数来实现。有关如何实现的任何答案?

英文:

I'm trying to set the attribute "Hour" to be between the numbers 0 and 23, How?
I'm trying to do it with a set function. So any answers to HOW?

public class Time {

private int Hour;
private int minute;

}//Class

答案1

得分: 0

如果我理解正确,您希望保护这些变量,以便它们不能被设置为无效值。所以 Hour 应该只能在 0 到 23 之间,而 minute 应该只能在 0 到 59 之间。

public class Time {
    private int Hour;
    private int minute;

    public int getHour() {
        return this.Hour;
    }
    
    public int getMinute() {
        return this.minute;
    }

    public void setHour(int hour) {
        // this.Hour = Math.min(Math.max(0, hour), 23);
        this.Hour = hour > 23 ? 23 : hour < 0 ? 0 : hour;
    }
    
    public void setMinute(int minute) {
        // this.minute = Math.min(Math.max(0, minute), 59);
        this.minute = minute > 59 ? 59 : minute < 0 ? 0 : minute;
    }
}
英文:

If I am understanding this correctly. You want to guard the variables so they cannot be set to invalid values. So Hour should only be between 0 and 23, and minute between 0 and 59.

public class Time {
    private int Hour;
    private int minute;

    public int getHour() {
        return this.Hour;
    }
    
    public int getMinute() {
        return this.minute;
    }

    public void setHour(int hour) {
        // this.Hour = Math.min(Math.max(0, hour), 23);
        this.Hour = hour &gt; 23 ? 23 : hour &lt; 0 ? 0 : hour;
    }
    
    public void setMinute(int minute) {
        // this.minute = Math.min(Math.max(0, minute), 59);
        this.minute = minute &gt; 59 ? 59 : minute &lt; 0 ? 0 : minute;
    }
}

答案2

得分: 0

如果您不希望使用数学,那么您可以在您的函数中添加简单的条件。

private int hour;
private int minute;

public void setHour(int value) {
    if (value > 23) {
        value = 23;
    }
    if (value < 0) {
        value = 0;
    }
    this.hour = value;
}

public void setMinute(int value) {
    if (value > 59) {
        value = 59;
    }
    if (value < 0) {
        value = 0;
    }
    this.minute = value;
}
英文:

If you don't wish to use Math then you can add simple condition into your function.

private int hour;
private int minute;

public void setHour(int value) {
    if (value &gt; 23) {
        value = 23;
    }
    if (value &lt; 0) {
        value = 0;
    }
    this.hour = value;
}

public void setMinute(int value) {
    if (value &gt; 59) {
        value = 59;
    }
    if (value &lt; 0) {
        value = 0;
    }
    this.minute = value;
}

huangapple
  • 本文由 发表于 2020年8月12日 07:09:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63367560.html
匿名

发表评论

匿名网友

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

确定