나는이 숙제를 통해 저를 곤란하게했습니다. 나는 24 시간 표시 thaot은 두 숫자 표시 개체로 구성되어 있습니다. numberDisplay 오브젝트에는 리미터가 있으므로, 60 분이되면 0으로 롤백되고 시간이 증가합니다. 시간이 24가되면 0으로 롤백됩니다.자바 시계는 두 개의 표시 객체로 구성됩니다.
24 시간 시계에서 12 시간 시계로 변경해야하지만이 개체에 대해서는 아무 것도 변경할 수 없습니다. 만 그들은 내가 지금까지 관리 한
상호 작용하는 방법 : 다시 제로 23 hours.value 변경, 시계가 다시 오전에 변경을 거부 그러나
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;
}
합니다. 나는 이것을 성취 할 수없는 방법을 찾지 못했다.
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;
}
}