Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 做一个;时钟“;它打印出军事时间并加/减时间_Java_Methods_Time_Tostring_Clock - Fatal编程技术网

Java 做一个;时钟“;它打印出军事时间并加/减时间

Java 做一个;时钟“;它打印出军事时间并加/减时间,java,methods,time,tostring,clock,Java,Methods,Time,Tostring,Clock,我正在尝试制作一个“时钟”,它可以做一些事情: 1) 将指定时间显示为军事时间(午夜为00:00:00) 2) 使用时钟对象(最多有3个整数参数(h、m、s)和数学方法或另一个时钟对象,向时钟添加或减去指定的时间量 --(示例) --例2) 所以我在两件事上遇到了麻烦:数学方法和toString()方法,这两种方法使所有内容都能按原样打印出来。我对toString()方法的概念还不熟悉,但它们还没有真正意义。下面是我的math和toString()方法,非常感谢您的帮助 //**********

我正在尝试制作一个“时钟”,它可以做一些事情:
1) 将指定时间显示为军事时间(午夜为00:00:00)
2) 使用时钟对象(最多有3个整数参数(h、m、s)和数学方法或另一个时钟对象,向时钟添加或减去指定的时间量
--(示例)

--例2)

所以我在两件事上遇到了麻烦:数学方法和toString()方法,这两种方法使所有内容都能按原样打印出来。我对toString()方法的概念还不熟悉,但它们还没有真正意义。下面是我的math和toString()方法,非常感谢您的帮助

//**********Methods to perform calculations w/ an object

    public void addHours(int h){
        if(this.hours + h > 23)
            hours = this.hours + (h % 24);
        else
            hours = this.hours + h;
    }

    public void addMinutes(int m){
        if(this.mins + m > 59){
            hours = this.hours + (m % 60);
            mins = this.mins + (hours % (m % 60));
        }
        else
            mins = this.mins + m;
    }

    public void addSeconds(int s){
        if(this.secs + s > 59){
            mins = this.mins + (s % 60);
            secs = this.secs + (mins % (s % 60));
        }
        else
            secs = this.secs + s;
    }

    public void addTime(int h, int m, int s){
        addHours(h);
        addMinutes(m);
        addSeconds(s);
    }


//Displays correctly formatted time
    public String toString(){
        if(hours > 23 || hours < 0)
            return "00:00:00";
        if(hours > 9)
            return "" + getHours();
        if(mins > 9)
            return "" + getMinutes();
        if(secs > 9)
            return "" + getSeconds();

        return "0" + getHours() + ":0" + getMinutes() + ":0" + getSeconds();
    }
使用对象执行计算的方法 公共空间附加小时数(整数小时){ 如果(本小时+h>23) 小时数=此点小时数+(小时数%24); 其他的 小时=这个。小时+小时; } 公共无效添加分钟数(整数m){ 如果(此.min+m>59){ 小时数=此点小时数+(m%60); 分钟=this.mins+(小时%(m%60)); } 其他的 分钟=this.mins+m; } 公共无效添加秒数(整数秒){ 如果(此.secs+s>59){ 分钟=this.mins+(s%60); secs=this.secs+(分钟%(s%60)); } 其他的 secs=this.secs+s; } 公共无效添加时间(整数h、整数m、整数s){ 增加小时(小时); 增加分钟(m); 添加秒数; } //显示格式正确的时间 公共字符串toString(){ 如果(小时数>23 | |小时数<0) 返回“00:00:00”; 如果(小时数>9) 返回“+getHours(); 如果(分钟>9) 返回“+getMinutes(); 如果(秒>9) 返回“+getSeconds(); 返回“0”+getHours()+”:0”+getMinutes()+“:0”+getSeconds(); }
您的
添加方法不必要且复杂。你可以简化它们。关于
toSting()
:在我看来,你对
return
有一个普遍的误解。
return
语句结束该方法。如果您的程序遇到
返回
,则不会执行该方法的其余代码。返回后面的语句(如果有)将被计算并返回

public void addHours(int h) {
    // The "if" was semantically unnecessary
    this.hours += (h % 24);
}

public void addMinutes(int m) {
        // Forward to addHours, exploit integer arithmetics
        this.addHours(m / 60);
        this.mins += (m % 60);
}

public void addSeconds(int s) {
    // Forward to addMinutes, exploit integer arithmetics
    this.addMinutes(s / 60);
    this.secs += (s % 60);
}
[...]
//Displays correctly formatted time
public String toString() {
    if ((this.hours > 23)  || (this.hours < 0)) {
        return ("00:00:00");
    }
    String result = "";
    // Add a leading zero
    if (this.hours < 10) {
        result += "0";
    }
    result += (this.hours + ":");

    // Add a leading zero
    if (this.mins < 10) {
        result += "0";
    }
    builder.append(this.mins + ":");

    // Add a leading zero
    if (this.secs < 10) {
        result += "0";
    }
    builder.append(this.secs);

    return (result);
}
public void addHours(inth){
//“if”在语义上是不必要的
这是1.4小时+=(小时数%24);
}
公共无效添加分钟数(整数m){
//转发到addHours,利用整数算法
这是增加小时数(m/60);
这1.5分钟+=(m%60);
}
公共无效添加秒数(整数秒){
//转发至addMinutes,利用整数算法
这项决议通过了15分钟(s/60);
这是1.secs+=(s%60);
}
[...]
//显示格式正确的时间
公共字符串toString(){
如果((this.hours>23)| |(this.hours<0)){
报税表(“00:00:00”);
}
字符串结果=”;
//添加前导零
如果(该时间小于10小时){
结果+=“0”;
}
结果+=(this.hours+“:”);
//添加前导零
如果(本秒<10){
结果+=“0”;
}
builder.append(this.mins+“:”);
//添加前导零
如果(本秒<10){
结果+=“0”;
}
builder.append(this.secs);
返回(结果);
}

编辑:理论上,可以将负值传递给
add
方法。如果你想安全起见,可以添加一些
If
-子句来防止时间减法。

谢谢你澄清了数学问题——我在这里添加了一些我没有展示的东西,以防止不真实的时间成为可能。不幸的是,我们还没有了解StringBuilder,我不认为我应该添加它,因为。。。嗯,我不仅不知道如何使用它,而且我还想以一种表明我理解当前材料的方式来使用它。但是我会看看你放了什么,并试着把它翻译成我现在知道的。@Mazzone制作一个
字符串
,而不是
StringBuilder
,并用
+
@Mazzone“附加”字符串。我编辑了代码以使用
字符串
,而不是
StringBuilder
。请看一看;在Java8中,使用时间API,否则使用JodaTimes如果您对使用先前存在的API不感兴趣,我会将时间保持为午夜后的秒数,这样更容易添加/减去值
//**********Methods to perform calculations w/ an object

    public void addHours(int h){
        if(this.hours + h > 23)
            hours = this.hours + (h % 24);
        else
            hours = this.hours + h;
    }

    public void addMinutes(int m){
        if(this.mins + m > 59){
            hours = this.hours + (m % 60);
            mins = this.mins + (hours % (m % 60));
        }
        else
            mins = this.mins + m;
    }

    public void addSeconds(int s){
        if(this.secs + s > 59){
            mins = this.mins + (s % 60);
            secs = this.secs + (mins % (s % 60));
        }
        else
            secs = this.secs + s;
    }

    public void addTime(int h, int m, int s){
        addHours(h);
        addMinutes(m);
        addSeconds(s);
    }


//Displays correctly formatted time
    public String toString(){
        if(hours > 23 || hours < 0)
            return "00:00:00";
        if(hours > 9)
            return "" + getHours();
        if(mins > 9)
            return "" + getMinutes();
        if(secs > 9)
            return "" + getSeconds();

        return "0" + getHours() + ":0" + getMinutes() + ":0" + getSeconds();
    }
public void addHours(int h) {
    // The "if" was semantically unnecessary
    this.hours += (h % 24);
}

public void addMinutes(int m) {
        // Forward to addHours, exploit integer arithmetics
        this.addHours(m / 60);
        this.mins += (m % 60);
}

public void addSeconds(int s) {
    // Forward to addMinutes, exploit integer arithmetics
    this.addMinutes(s / 60);
    this.secs += (s % 60);
}
[...]
//Displays correctly formatted time
public String toString() {
    if ((this.hours > 23)  || (this.hours < 0)) {
        return ("00:00:00");
    }
    String result = "";
    // Add a leading zero
    if (this.hours < 10) {
        result += "0";
    }
    result += (this.hours + ":");

    // Add a leading zero
    if (this.mins < 10) {
        result += "0";
    }
    builder.append(this.mins + ":");

    // Add a leading zero
    if (this.secs < 10) {
        result += "0";
    }
    builder.append(this.secs);

    return (result);
}