java - What am I doing wrong in this code? It's supposed to run indefinitely, but only loops twice -
i have final string array @ top labeled jokes. program ask if user wants hear joke, tell user joke, , ask if hear another. of right now, program ask if user wants hear joke, tell them joke, ask them if hear another, tell them second joke, , stop. program indefinitely opposed twice. or advice appreciated, i'm still beginner java.
random generator = new random(); input = new scanner(system.in); system.out.println("want hear joke?"); string response = "yes"; while (response.equalsignorecase("yes")) { response = input.nextline(); int yourjoke = generator.nextint(jokes.length); system.out.println(jokes[yourjoke]); system.out.println("pretty funny! want hear another?"); response = input.nextline(); } }
}
- you should call
input.nextline()
once inside of while loop. - why ask input before while loop if you're going ignore it, if you're going force response "yes" regardless of user's choice?
one solution in pseudocode:
initialize scanner initialize random hearanother = false prompt: want hear joke? input hearanother = input ignore case "yes" if hearanother tell joke prompt: want hear joke? input hearanother = input ignore case "yes" if hear tell random joke end if while hearanother true end if
Comments
Post a Comment