java - error about returning a string from a String method -
this question has answer here:
- how convert number words in java 23 answers
why getting error in both of these methods 'this method must return type string...when returning type string. trying stupid ninety-nine bottles of beer on wall question i'm sure had learning java. in book instead of outputting numbers (ie.99) has print out words(ninety-nine). tried break down in these 2 methods, saying needs return string , are. had them bracketed out, nothing changed.
public string rounds() { if(beer>89) return "ninety"; else if(beer>79) return "eighty"; else if(beer>69) return "seventy"; else if (beer>59) return("sixty"); else if (beer>49) return ("fifty"); else if (beer>39) return("forty"); else if(beer>29) return("thirty"); else if(beer>19) return("twenty"); else if(beer==19) return "nineteen"; else if(beer==18) return "eighteen"; else if (beer==17) return("seventeen"); else if (beer==16) return ("sixteen"); else if (beer==15) return("fifteen"); else if(beer==14) return("fourteen"); else if(beer==13) return("thirteen"); else if(beer==12) return("twelve"); else if(beer==11) return("eleven"); else if(beer==10) return("ten"); } private string ones() { if(beer % 10==9) return("-nine"); else if(beer % 10==8) return("-eight"); else if(beer % 10==7) return("-seven"); else if(beer % 10==6) return("-six"); else if(beer % 10==5) return("-five"); else if(beer % 10==4) return("-four"); else if(beer % 10==3) return("-three"); else if(beer % 10==2) return("-two"); else if(beer % 10==1) return("-one"); else if(beer % 10==0) return("zero"); } }
because "if-else" block not covering every condition. think method "rounds" return if "beer" value equals '9'? have add "else" block @ , one;
else // have cover conditions return "none"; // should add these
complete solution below;
public class testreturn { public static void main(string[] args) { int beer = 69; system.out.println( beer + " : " + rounds(beer) + ones(beer)); } public static string rounds(int beer) // beer must declared either parameter or local variable { if (beer > 89) return "ninety"; else if (beer > 79) return "eighty"; else if (beer > 69) return "seventy"; else if (beer > 59) return ("sixty"); else if (beer > 49) return ("fifty"); else if (beer > 39) return ("forty"); else if (beer > 29) return ("thirty"); else if (beer > 19) return ("twenty"); else if (beer == 19) return "nineteen"; else if (beer == 18) return "eighteen"; else if (beer == 17) return ("seventeen"); else if (beer == 16) return ("sixteen"); else if (beer == 15) return ("fifteen"); else if (beer == 14) return ("fourteen"); else if (beer == 13) return ("thirteen"); else if (beer == 12) return ("twelve"); else if (beer == 11) return ("eleven"); else if (beer == 10) return ("ten"); else // have cover conditions return "none"; // should add these } private static string ones(int beer) // beer must declared either parameter or local variable { if (beer % 10 == 9) return ("-nine"); else if (beer % 10 == 8) return ("-eight"); else if (beer % 10 == 7) return ("-seven"); else if (beer % 10 == 6) return ("-six"); else if (beer % 10 == 5) return ("-five"); else if (beer % 10 == 4) return ("-four"); else if (beer % 10 == 3) return ("-three"); else if (beer % 10 == 2) return ("-two"); else if (beer % 10 == 1) return ("-one"); else if (beer % 10 == 0) return ("zero"); else // have cover conditions return "none"; // should add these } }
and here output;
69 : sixty-nine
Comments
Post a Comment