gpt4 book ai didi

java - 无法为 boolean 方法选择正确的形式参数

转载 作者:行者123 更新时间:2023-12-01 10:09:24 26 4
gpt4 key购买 nike

在评论版本 4 下,我尝试创建一个名为 equals 的方法来测试小时、分钟和秒。 return 语句中再次使用形参。我知道我应该采用 ______.hours 格式,hours 是用于测试和生成 true 或 false 的实例变量,但我不知道句点之前应该作为形式参数。任何建议/解释将不胜感激。

public class Clock
{
private static final byte DEFAULT_HOUR = 0,
DEFAULT_MIN = 0,
DEFAULT_SEC = 0,
MAX_HOURS = 24,
MAX_MINUTES = 60,
MAX_SECONDS = 60;

// ------------------
// Instance variables
// ------------------

private byte seconds,
minutes,
hours;

public Clock (byte hours , byte minutes , byte seconds )
{
setTime(hours, minutes, seconds);
}

public Clock ( )
{
setTime(DEFAULT_HOUR, DEFAULT_MIN, DEFAULT_SEC);
}

public void setTime ( byte hours, byte minutes, byte seconds )
{
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
// hours
if (DEFAULT_HOUR >= 0 && DEFAULT_HOUR <= 29)
{

}

else
{
hours = DEFAULT_HOUR;
}
// minutes
if (DEFAULT_MIN >= 0 && DEFAULT_MIN <= 59)
{

}

else
{
minutes = DEFAULT_MIN;
}
// seconds
if (DEFAULT_SEC >= 0 && DEFAULT_SEC <= 59)
{

}

else
{
seconds = DEFAULT_SEC;
}


}
//--------------------------
// Version 3 mutator methods
//--------------------------
public void incrementSeconds()
{
seconds += 1;
if (seconds >= 59)
{
seconds = DEFAULT_SEC;
incrementMinutes();
}

}

public void incrementMinutes()
{
minutes += 1;
if (minutes >= 59)
{
minutes = DEFAULT_MIN;
incrementHours();
}
}

public void incrementHours()
{
hours += 1;
if (hours >= 23)
{
hours = DEFAULT_HOUR;
}
}

//----------
// Version 4
//----------

public boolean equals(Clock your_clock)
{
return boolean your_clock.hours;
}



//----------
// Version 2
//----------

public String toString()

{


final byte MIN_2DIGITS = 10;

String str = "";



// my input

if (hours < MIN_2DIGITS)
{
str += "0" + hours + ":" ;
}
else
str += hours + ":";
if (minutes < MIN_2DIGITS)
{
str += "0" + minutes + ":" ;
}
else
str += minutes + ":";
if (seconds < MIN_2DIGITS)
{
str += "0" + seconds;
}
else
str += seconds;



//end of my input

return str;

}


} // End of class definition

最佳答案

如果您试图找到参数 Clock 和调用者 Clock 之间的相等性,我会执行以下操作

public boolean equals(Clock another_clock) {
// Check if 'this' is equal to 'another_clock'

// 1. If you're checking if the pointer is the same
return another_clock.equals(this);

// 2. If you're checking if time is the same (You probably need to create getter/setter methods or change privacy for these fields)
return another_clock.hours == this.hours &&
another_clock.minutes == this.minutes &&
another_clock.seconds == this.seconds;
}

或者类似的东西。

关于java - 无法为 boolean 方法选择正确的形式参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230925/

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