简体   繁体   English

Java中的时钟滴答变化

[英]Clock tick change in Java

OK, so i'm new to this and have just signed up however i need some help with explanations please.. 好的,所以我是新来的,刚刚注册但是我需要一些帮助解释请...

I have a assignment which has asked me to convert a 24 hour clock into a 12 hour clock by making some adjustments. 我有一项任务要求我通过做一些调整将24小时制转换为12小时制。 I am pretty sure I am almost there however I cannot get the boolean to switch when the hour changes using the timeTick method in the code. 我很确定我几乎就在那里但是当代码中使用timeTick方法改变小时时我无法获得布尔值。 To be honest I believe the rest is fine but any help would be appreciated: 说实话,我相信其余的都很好,但任何帮助将不胜感激:

    public class ClockDisplay
{
    private NumberDisplay hours;
    private NumberDisplay minutes;
    private String displayString; 
    private boolean isAM;


/**
 * Constructor for ClockDisplay objects. This constructor 
 * creates a new clock set at 00:00.
 */
public ClockDisplay()
{
    hours = new NumberDisplay(12);
    minutes = new NumberDisplay(60);
    updateDisplay();
    setMorn();
}

/**
 * Constructor for ClockDisplay objects. This constructor
 * creates a new clock set at the time specified by the 
 * parameters.
 */
public ClockDisplay(int hour, int minute)
{
    hours = new NumberDisplay(12);
    minutes = new NumberDisplay(60);
    setTime(hour, minute);
    setMorn();
 }

/**
 * This method should get called once every minute - it makes
 * the clock display go one minute forward.
 */
public void timeTick()
{
    minutes.increment();
 if(minutes.getValue() == 0) {  // it just rolled over!
        hours.increment();
 }
 if (hours.getValue() == 12)
 {
     isAM = !isAM;
 }

     updateDisplay();
}

private void setMorn()
{
        isAM = true;
}
private void setAft()
{
        isAM = false;   
}

/**
 * Set the time of the display to the specified hour and
 * minute.
 */
public void setTime(int hour, int minute)
{   
    hours.setValue(hour);
    minutes.setValue(minute);
    updateDisplay();
}

/**
 * Return the current time of this display in the format HH:MM.
 */
public String getTime()
{
    return displayString;
}


/**
 * Update the internal string that represents the display.
 */
private void updateDisplay()
{
    int hour = hours.getValue();
    String daynight;
    if (isAM = true)
    {
    daynight = "AM";
    if (hour == 0) 
    {
     hour = 12;   
    }
}
else
{
    isAM = false;
    daynight = "PM";
    if (hour == 0) 
    {
     hour = 12;   
    }
}
    displayString = hour + ":" + 
    minutes.getDisplayValue() + daynight;


  }
}

What We have 我们有什么

  1. Add a boolean field isAM which will have the value true (it is morning) and false (it is afternoon). 添加一个布尔字段isAM,其值为true(早上)和false(下午)。
  2. We create two new methods; 我们创造了两种新方法; setMorning which sets isAM to be true and setAfternoon which sets isAM to be false. setMorning将isAM设置为true,setAfternoon将isAM设置为false。
  3. Both constructors will initialise the time of day to be morning by default and so both invoke setMorning. 默认情况下,两个构造函数都会将一天的时间初始化为早晨,因此两者都会调用setMorning。
  4. In timeTick we need to check whether the hours rolled over, meaning it either changed from am to pm or from pm to am. 在timeTick中,我们需要检查小时是否翻过来,这意味着它要么从上午改为下午,要么从下午改为上午。 Note the use of: isAM = !isAM 注意使用:isAM =!isAM
  5. In updateDisplay we need to create the suffix "am" or "pm" depending on whether isAM is true or false. 在updateDisplay中,我们需要创建后缀“am”或“pm”,具体取决于isAM是true还是false。

Your problem is almost certainly in the line that reads: 您的问题几乎肯定在以下内容中:

if (isAM = true)

This is actually setting isAM to true and the result of the expression is therefore also true so the else part will never be executed. 这实际上是将isAM设置为true ,因此表达式的结果也是true因此else部分永远不会被执行。

You probably meant: 你可能意味着:

if (isAM == true)

or - better still: 或者 - 更好的是:

if (isAM)

OK, so i finally worked it out thanks to Dragondraikk who made me think. 好的,所以我终于解决了,感谢Dragondraikk让我思考。 The issue was just as he suggested, well kind of. 这个问题就像他建议的那样,很好。 as I set the hours to 0 - 11 and tick over. 因为我将小时数设置为0 - 11并勾选。 I was not changing 0 into a 12 until the updateDisplay was run so it was actually a 0 and not a twelve: 在updateDisplay运行之前,我没有将0更改为12,所以它实际上是0而不是12:

 public void timeTick()
    {
     minutes.increment()
     if(minutes.getValue() == 0) {  // it just rolled over!
         hours.increment(); 
        }
     if (hours.getValue() == 0)
         {
         isAM = !isAM;   
        }   
    updateDisplay();
    }

This fixed the problem and now it works :) thank you everyone for all your help.I am also aware that I can provide feedback in the form of a rating, if someone can tell me how I am more than happy to do so :) 这解决了问题,现在它的工作原理:)谢谢大家的帮助。我也知道我可以以评级的形式提供反馈,如果有人能告诉我我是多么乐意这样做:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM