We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticStringtimeInWords(inth,intm){String[]nums={"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","quarter","sixteen","seventeen","eighteen","nineteen"};if(m==0){returnnums[h-1]+" o' clock";}elseif(m<30){returntextTime(m,h,nums,"past");}elseif(m==30){return"half past "+nums[h-1];}else{returntextTime(60-m,h+1,nums,"to");}}privatestaticStringtextTime(intm,inth,String[]nums,Stringprep){StringJoinerjoiner=newStringJoiner(" ");inttens=m/10;if(m==20){joiner.add("twenty");}elseif(tens==2){intrest=m%10;joiner.add("twenty");joiner.add(nums[rest-1]);}else{joiner.add(nums[m-1]);}if(m==1){joiner.add("minute");}elseif(m!=15){joiner.add("minutes");}joiner.add(prep).add(nums[h-1]);returnjoiner.toString();}
Cookie support is required to access HackerRank
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 →
JAVA