You are viewing a single comment's thread. Return to all comments →
Java 8
public static String timeInWords(int h, int m) { final String hours[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"}; final String[] minutes = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty one", "twenty two", "twenty three", "twenty four", "twenty five", "twenty six", "twenty seven", "twenty eight", "twenty nine", }; // Write your code here if(m == 0) return hours[h]+ " " +"o' clock" ; if(m == 15) return "quarter" + " " + "past"+ " " + hours[h] ; if(m == 30) return "half" + " " + "past"+ " " + hours[h] ; if(m == 45){ if(h == 12) return "quarter to"+ " " + hours[1] ; return "quarter to"+ " " + hours[h+1] ; } if( m < 30){ if(m < 10) return minutes[m]+" "+ "minute" + " " + "past" + " " + hours[h]; return minutes[m]+" "+ "minutes" + " " + "past" + " " + hours[h]; } if(m > 30 && h == 12) return minutes[60-m]+ " "+ "minutes" + " " + "to" + " " + hours[1] ; return minutes[60-m]+ " " + "minutes" + " " + "to" + " " + hours[h+1] ; }
Seems like cookies are disabled on this browser, please enable them to open this website
The Time in Words
You are viewing a single comment's thread. Return to all comments →