This class is supposed to create a clock and I'm trying to make a method so you can advance x amount of minutes. The method works fine except for when the initial hour is 12:00, when this happens I always get AM/PM wrong.
public void advance(int a){
int m =a%60; //Minutes to add
int h =a/60; //Hours to add
minute = minute + m; //Add minutes to m
while(minute>=60) { //Divides minutes by 60 to get the amount of hours in case the input is bigger than 60
minute = minute - 60;
h++;
}
// Test if OG hour = 12
if(hour==12){
hour=0;
if(h<12) {
amPm = amPm;
hour = hour + h;
}
if(h>=12)
if(amPm.equals("AM")) {
amPm = "PM";
hour = hour + h;
}
else
if(amPm.equals("AM")) {
amPm = "PM";
hour = hour + h;
}
if(hour==0)
hour=12;
} else {
hour = hour + h;
while(hour>12) {
if(amPm.equals("AM")) {
amPm = "PM";
} else
amPm = "AM";
hour = hour - 12;
}
}
}
Aucun commentaire:
Enregistrer un commentaire