Logic Problem JAVA Clock Program

Asked

Viewed 34 times

0

Good afternoon my dear I’m having a problem with the logic of this program which is a clock:

private void updateDisplay() {

        if(hours.getValue()==0 && (periodo=="am" || periodo =="AM")){
              hours.setValue(hours.getValue()+1);
              periodo = "am";
        }
        else{
            if(hours.getValue()==12 && (periodo=="am" || periodo =="AM")){
                periodo="pm";
            }
            else{

            }
        }
        if(hours.getValue()==0 && (periodo=="pm" || periodo =="PM")){
                hours.setValue(hours.getValue()+1);
                periodo="pm";
        }
        else{

        }


         displayString = hours.getDisplayValue() + ":" + 
                    minutes.getDisplayValue() + " " + periodo; 
    }
}

All functions are perfect really is just the part of logic that’s complicated. And I want to limit between 1-12 so when it reaches the limit that is 11:59 pm it turns 12:00 am and when it arrives 11:59 am it turns 12:00 pm.

2 answers

0

Try this:

if(hours.getValue()==0 && (periodo.equals("am") || periodo.equals("AM")){

        hours.setValue(hours.getValue()+1);
        periodo = "am";

    }else if(hours.getValue()==12 && (periodo.equals("pm") || periodo.equals("PM")){

        hours.setValue(hours.getValue()+1);
        periodo = "pm";

    }else if(hours.getValue()==0 && (periodo.equals("pm") || periodo.equals("PM")){

        hours.setValue(hours.getValue()+1);
        periodo="pm";

    }else if (hours.getValue()==12 && (periodo.equals("am") || periodo.equals("AM")){

        hours.setValue(hours.getValue()+1);
        periodo="am";

    } 

I hope this helps!

  • example when it comes to 11:58 am and I call the minute jump method goes to 11:59 am and repeating again goes to 12:00 am being q should be pm because these two iff so conflicting I guess there prevails the am q is the last qnd is in the 12 hour period.

0

You can have another function that will serve as a flag: Every time it reaches the value of 12:00, it will call this function: If her previous value is AM, this function will resume PM. If its previous value is PM, this function will resume AM.

something like

if (hours.getDisplayValue()==12 && minutes.getDisplayValue()==00){
    stringAmPm=verificaAmPm(stringAmPm)
        if (stringAmPm = "AM"){
            escreve que está de dia}
        else  {
            escreve que está de noite
        }
    }


private verificaAmPm (stringAmPm)
    if (stringAmPm = "AM") {
        stringAmPm = "PM"
    }
    else {
        stringAmPm = "AM"
    }
    return stringAmPm
  • I’ll try to implement

  • 1

    ae I got @Roni Antonio doing it your way :D vlw same

Browser other questions tagged

You are not signed in. Login or sign up in order to post.