我有一项家庭作业使我很为难。我有一个24小时时钟,是由两个numberDisplay对象组成的。numberDisplay对象有一个限制器,可以对它们进行角色设置,因此当分钟数达到60分钟时,它会回滚到零,并增加小时数。当24小时到达时,它会回到零。
我的任务是把它从24小时的时钟改为12小时的时钟,但是我不能改变有关对象的任何东西。只有他们如何相互作用
到目前为止,我已经做到了:
private void updateDisplay()
{
int functionHours = Integer.parseInt(hours.getDisplayValue());
String amPM = "AM";
if ( functionHours == 0 ) {
hours.setValue(12);
amPM = "AM";
}
else if ( functionHours > 0 && functionHours < 12 )
{
hours.setValue(functionHours);
amPM = "AM";
}
else if ( functionHours == 12 ) {
hours.setValue(functionHours);
amPM = "PM";
}
else if ( functionHours > 12 && functionHours <= 23 ) {
functionHours -= 12;
hours.setValue(functionHours);
amPM ="PM";
}
displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue() + " " + amPM;
}然而,当hours.value从23变回零时,时钟拒绝更改回AM我很难完成这件事。
NumberDisplay对象:
/**
* The NumberDisplay class represents a digital number display that can hold
* values from zero to a given limit. The limit can be specified when creating
* the display. The values range from zero (inclusive) to limit-1. If used,
* for example, for the seconds on a digital clock, the limit would be 60,
* resulting in display values from 0 to 59. When incremented, the display
* automatically rolls over to zero when reaching the limit.
*
* @author Michael Kölling and David J. Barnes
* @version 2011.07.31
*/
public class NumberDisplay
{
private int limit;
private int value;
/**
* Constructor for objects of class NumberDisplay.
* Set the limit at which the display rolls over.
*/
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
/**
* Return the current value.
*/
public int getValue()
{
return value;
}
/**
* Return the display value (that is, the current value as a two-digit
* String. If the value is less than ten, it will be padded with a leading
* zero).
*/
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
/**
* Set the value of the display to the new specified value. If the new
* value is less than zero or over the limit, do nothing.
*/
public void setValue(int replacementValue)
{
if((replacementValue >= 0) && (replacementValue < limit)) {
value = replacementValue;
}
}
/**
* Increment the display value by one, rolling over to zero if the
* limit is reached.
*/
public void increment()
{
value = (value + 1) % limit;
}
}发布于 2013-02-28 06:01:08
考虑一下如果代码中有hours.getDisplayValue() == 23会发生什么。在最后一个块中,您可以这样修改它:
// functionHours is 23.
else if ( functionHours > 12 && functionHours <= 23 ) { // OK
functionHours -= 12; // now functionHours is 11
hours.setValue(functionHours); // set hours.value to 11 !!
amPM ="PM";下一个更新,可能是hours.value增加了一个,然后它的值将是12。但是在您的代码中,该值必须小于12才能使amPM变成"AM“。这就是为什么它停留在“下午”。
我并不完全理解您的需求,但是您可能不应该修改hours对象中的值,而应该使用装饰器模式来在其上添加12小时的功能。
发布于 2013-02-28 06:33:21
谢谢你的逻辑,说得通。我是这样做的:
/**
* Update the internal string that represents the display.
*/
private void updateDisplay()
{
int functionHours = Integer.parseInt(hours.getDisplayValue());
if ( functionHours == 0 ) {
displayString = "12:" + minutes.getDisplayValue() + " AM";
}
else if ( functionHours >= 0 && functionHours < 12 ) {
displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue() + " AM";
}
else if ( functionHours == 12 ) {
displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue() + " PM";
}
else if ( functionHours >= 12 && functionHours < 22 ) {
int tempHours = ( hours.getValue() - 12 );
displayString = "0" + Integer.toString(tempHours) + ":" + minutes.getDisplayValue() + " PM";
}
else if ( functionHours >21 && functionHours < 24 ) {
displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue() + " PM";
}
}https://stackoverflow.com/questions/15128256
复制相似问题