此类ClockDisplay实现12小时数字时钟。
public class NumberDisplay
{
//instance variables
private int state;
private int limit;
public NumberDisplay(int rollOverLimit)
{
state = 0;
setLimit(rollOverLimit);
}
public int getState()
{
return state;
}
public int getLimit()
{
return limit;
}
public void setState(int replacementValue)
{
if( replacementValue >= 0 && replacementValue < limit)
state = replacementValue;
}
public void setLimit(int rollOverLimit)
{
// if (rollOverLimit > 0)
// limit = rollOverLimit;
limit = (rollOverLimit > 0 ? rollOverLimit : limit);
state = ( state > limit ? 0 : state);
}//of setLimit
public void increment()
{
/*
if (state == limit-1)
state = 0;
else
state++;
*/
state = (state+1) % limit;
}
public String getDisplayValue()
{
if(state < 10){
return "0" + state;
}
else{
return ""+ state;
}
}
}
public class ClockDisplay {
// instance variables
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
private boolean isAM;
public String displayAMPM;
// private int count = 0;
public ClockDisplay() {
hours = new NumberDisplay(12);
minutes = new NumberDisplay(60);
updateDisplay();
}
public ClockDisplay(int hour, int minute, String displayAMPM) {
hours = new NumberDisplay(12);
minutes = new NumberDisplay(60);
setTime(hour, minute, displayAMPM);
//setMorning(isAM);
//count = 0;
updateDisplay();
}
public void timeTick() {
minutes.increment();
if (minutes.getState() == 0) {
hours.increment();
}
if (hours.getState() == 12) {
if (displayAMPM == "AM") {
displayAMPM = "PM";
} else if (displayAMPM == "PM") {
displayAMPM = "AM";
}
}
//if (isAM != isAM)
//displayAMPM = "PM";
updateDisplay();
}//of timeTick
public void setTime(int hour, int minute, String displayAMPM) {
if (hour == 12) {
hour = 0;
}
hours.setState(hour);
minutes.setState(minute);
updateDisplay();
}
public void setHours(int hour) {
hours.setState(hour);
updateDisplay();
}
public void setMinutes(int minute) {
minutes.setState(minute);
updateDisplay();
}
/*public void setMorning(boolean isAM)
{
isAM = true;
displayAMPM = "AM";
}
public void setAfternoon(boolean isAM)
{
isAM = false;
displayAMPM = "PM";
}
*/
private void updateDisplay() {
int hour = hours.getState();
if (hour == 0) {
hour = 12;
}
displayString = hour + ":" + minutes.getDisplayValue() + displayAMPM;
}
public String getTime() {
return displayString;
}
}
public class Tester {
public static void main(String[] args) throws Exception {
ClockDisplay clock12 = new ClockDisplay(11, 59, "AM");
System.out.println(clock12.displayAMPM);
System.out.println(clock12.getTime());
while (true) {
Thread.currentThread().sleep(60000);
clock12.timeTick();
System.out.println(clock12.getTime());
}
}
}
上述程序是带有AM / PM显示的12小时时钟 如果我给出输入&#34; AM&#34;变量displayAMPM被视为空。或&#34; PM&#34;
Output: null
11:59null
12.00null
我不确定这个程序出了什么问题。有人可以帮我这个吗?
答案 0 :(得分:0)
在初始化时,您永远不会更改displayAMPM
的值。您应该在构造函数中执行此操作,在本例中,在您调用的方法中:
public void setTime(int hour, int minute, String displayAMPM) {
if (hour == 12) {
hour = 0;
}
hours.setState(hour);
minutes.setState(minute);
this.displayAMPM = displayAMPM; // 'this.displayAPMP' will refer
// to the instance attribute 'displayAMPM'
updateDisplay();
}
另外,请勿使用==
运算符来比较字符串。由于它们是对象,并且您想要比较其内容(而不是其引用),因此您应该使用.equals()
。
答案 1 :(得分:0)
我认为在调用构造函数时你没有设置displayAMPM字符串。
根据我的意见,你必须在调用构造函数时初始化该字符串,否则它将不会设置为hours.getState()==“12”你已经检查了以前未设置的值,因此它不会设置为任何值。
希望这个建议可以帮到你。