gpt4 book ai didi

Java时钟分配

转载 作者:行者123 更新时间:2023-11-29 03:41:38 26 4
gpt4 key购买 nike

我的任务是制作一个时钟。我们需要小时、分钟和秒的变量和方法,如 setHours/getHours、setMinutes/getMinutes、setSeconds/getSeconds。我还需要一个 addClock() 方法来计算两个时钟对象的总和,以及一个递减时钟的 tickDown() 方法。我还需要一个 tick() 方法来将 Clock 对象递增一秒。

这是我目前所拥有的...

public class Clock {

private int hr; //store hours
private int min; //store minutes
private int sec; //store seconds

// Default constructor

public Clock () {
setClock (0, 0, 0);
}

public Clock (int hours, int minutes, int seconds) {
setClock (hours, minutes, seconds);
}

public void setClock (int hours, int minutes, int seconds) {
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

public int getHours() {
return hr;
}

public int getMinutes() {
return min;
}

public int getSeconds() {
return sec;
}

public void addClock( Clock secondClock ) {
this.sec += secondClock.getSeconds();
this.min += secondClock.getMinutes();
//add overflow to minutes from seconds
this.min +=(int)(this.sec/60);
//update seconds
this.sec = this.sec % 60;
this.hr += secondClock.getHours();
//add overflow to minutes from seconds
this.hr +=(int)(this.min/60);
//update minutes
this.min = this.min % 60;
//adjust hours
this.hr = this.hr % 24;
}

public void tick(){
this.sec += 1;
//add overflow to minutes from seconds
this.min +=(int)(this.sec/60);
//update seconds
this.sec = this.sec % 60;
//add overflow to minutes from seconds
this.hr +=(int)(this.min/60);
//update minutes
this.min = this.min % 60;
//adjust hours
this.hr = this.hr %24;
}

public void tickDown(){
this.sec -= 1;
if(this.sec <0){
this.sec+=60;
this.min-=1;
}
if(this.min<0){
this.min+=60;
this.hr-=1;
}
if(this.hr<0){
this.hr+=24;
}
}
}

最佳答案

你可以这样写addClock():

    public void addClock(Clock secondClock){
this.second += secondClock.getSecond();
this.minute+= secondClock.getMinute();
//add overflow to minutes from seconds
this.minute+=(int)(this.second/60);
//update seconds
this.second = this.second % 60;
this.hour += secondClock.getHour();
//add overflow to minutes from seconds

this.hour+=(int)(this.minute/60);
//update minutes
this.minute= this.minute% 60;

//adjust hours
this.hour = this.hour%24;
}

类似地,您可以编写ticktickDown 方法,唯一的区别是方法中没有任何参数,您需要重新调整分钟和小时秒数加减1后

  public void tick(){
this.second += 1;
//add overflow to minutes from seconds
this.minute+=(int)(this.second/60);
//update seconds
this.second = this.second % 60;
//add overflow to minutes from seconds
this.hour+=(int)(this.minute/60);
//update minutes
this.minute= this.minute% 60;
//adjust hours
this.hour = this.hour%24;
}

public void tickDown(){
this.second -= 1;
if(this.second <0){
this.second+=60;
this.minute-=1;
}
if(this.minute<0){
this.minute+=60;
this.hour-=1;
}
if(this.hour<0){
this.hour+=24;
}
}

main 方法添加为:

   public static void main(String[]  args){
Clock clock1 = new Clock(2,4,7);
Clock clock2 = new Clock(8,26,57);
clock1.tick();
int newSeconds = clock1.getSeconds();
//validate it should be 8 now

clock2.tickDown();
newSeconds = clock1.getSeconds();
//validate it should be 56 now

clock1.addClock(clock2);
//get Hour, minute and second from clock1 and validate
int newHour = clock1.getHours();
int nuwMinute = clock1.getMinutes();
newSeconds = clock1.getSeconds();
//validate if they are with the right expected value or not
}

关于Java时钟分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12862017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com