2013-02-28 1 views
1

나는이 숙제를 통해 저를 곤란하게했습니다. 나는 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; 
    } 
} 

답변

2

코드에서 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이 될 것입니다. 하지만 코드에서 amPM이 "AM"이 되려면 해당 값이 12보다 작아야합니다. 그것이 "PM"에 머물러있는 이유입니다.

사용자의 요구 사항을 정확하게 이해하지 못했지만 hours 개체의 값을 수정하지 말고 대신 장식 자 패턴을 사용하여 12 시간짜리 기능을 추가해야합니다.

0

논리에 감사드립니다. 나는 이렇게했다 :

/** 
* 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"; 
    }  
} 
관련 문제