java - Unused String Variable -
import java.util.scanner; public class main { public static void main(string[] args) { scanner input = new scanner(system.in); system.out.println("enter name of favorite insect: "); string userstring = input.next(); string lyric = ""; string endlyric = ""; string onethree = "i'm " +lyric+ "my baby " +userstring; string 2 = "won't mommy proud of me"; int count = 5; while (count != 0) { if (count == 5) { lyric = "bringing home "; endlyric = "ouch!! stung me!!"; count = count - 1; } else if (count == 4) { lyric = "squishin' "; endlyric = "ick!! feel sick!!"; count = count - 1; } else if (count == 3) { lyric = "barfin "; endlyric = "oh!! mess!!"; count = count - 1; } else if (count == 2) { lyric = "wipin' "; endlyric = "oops!! mommy's new towel!!"; count = count - 1; } else if (count == 1) { lyric = "wringin' "; endlyric = "bye-bye baby "+userstring+ "!!"; count = count - 1; } system.out.println(onethree); system.out.println(two); system.out.println(onethree); system.out.println(endlyric); system.out.println(""); } } }
for odd reason lyric variable not print... error states unused. have initialized variable. other variables work fine 1 please help...
you not using lyric
variable anywhere in code after assigning value. use before initialized non empty value.
string onethree = "i'm " +lyric+ "my baby " +userstring;
become "i'm baby " +userstring;
, since @ point it's assigned, lyric
empty. fact change lyric
later non empty doesn't make difference, since never access again after assignment.
you initialize onethree
:
string onethree = "i'm {0} baby " +userstring;
and then, instead of system.out.println(onethree);
system.out.println(string.format(onethree,lyric));
. put current value of lyric
in string.
Comments
Post a Comment