English 中文(简体)
用蓝J把24小时换成12
原标题:Changing a 24 clock to a 12 using BlueJ
private void updateDisplay()
{   
    if(hours.getValue() == 0)
    {
        hours.setValue(12);
        displayString = hours.getDisplayValue() + ":" + 
        minutes.getDisplayValue() + " am"; 
    }
    else if(hours.getValue() < 12)
    {
        displayString = hours.getDisplayValue() + ":" + 
        minutes.getDisplayValue() + " am";
    }
    else if(hours.getValue() == 12)
    {
        displayString = hours.getDisplayValue() + ":" + 
        minutes.getDisplayValue() + " pm";
    }
    else if(hours.getValue() < 24)
    { 
        displayString = Integer.toString(hours.getValue() - 12) + ":" +  
        minutes.getDisplayValue() + " pm"; 
    }
}

我只能用这个方法来改变时钟的显示 。 我工作了好几个小时, 但我却被卡住了, 因为基于某种原因, 在这种方法中, 它总是跳到其他语句上, 尽管输入的数值符合要求。 下面我将显示我使用的其它类的相关部分 。 现在编辑在午夜滚动时不会留在 AM 中 。

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;
    }
}
问题回答

我想你错过了一个 else :

private void updateDisplay()
{
  if(hours.getValue() < 12)
     displayString = hours.getDisplayValue() + ":" +
            minutes.getDisplayValue() + " am";

 [this one] >>>> else if(hours.getValue() >= 12 && hours.getValue() < 25)
     displayString = Integer.toString(hours.getValue() - 12) + ":" + 
            minutes.getDisplayValue() + " pm";

  else {
    hours.setValue(12);
    displayString = hours.getDisplayValue() + ":" + 
                    minutes.getDisplayValue() + " am";
  }
}

<强 > 添加:

您也可以使用"http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SempleDateFormat.html" rel=“nofollow”>SupreDateFormat 来格式化您的时间。

SimpleDateFormat from = new SimpleDateFormat("H");
SimpleDateFormat to = new SimpleDateFormat("h a");

return to.format(from.parse(hours.getValue));

<强 > Added2:

如果您需要 < 坚固 > 来手动计算, 最简单的方式就是 :

if (hours.getValue() == 0) {
    return "12 am";
} else if (hours.getValue() < 12) {
    return hours.getValue() + " am";
} else if (hours.getValue() == 12) {
    return "12 pm";
} else if (hours.getValue() < 24) {
    return (hours.getValue()-12) + " pm";
} else {
    throw new ParseException("Invalid hours value: "+hours.getValue());
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签