java - Problems with generating another "Joke" Statement -
currently, creating simple program tells jokes users.
bot: how can 4 suits dollar? type reveal view answer
you: reveal
bot: buy deck of cards. giggle giggle
bot: wish continue? yes or no?
you: yes
bot: call boomerang won't come back? type reveal view answer
that intended script. however, faced problem:
bot: wish continue? yes or no?
you: yes
bot: wrong >:d
the bot print out "wrong" instead of generating joke. here code:
public class newclass { public static void main(string[] args) { int randomnumber; string userinput; string[] jokesanswer = {"snowballs! haha ", "a stick!!! :d", "a pork chop.", "a piano! hehe ", "a dead centipede!! xd", "at crystal ball. *chuckle chuckle*", "a conversation. teehee", "buy deck of cards. *giggle giggle*"}; string[] jokes = {"what difference between snowman , snowwoman?", "what call boomerang won't come back?", "what call pig karate?", "what has lot of keys can not open doors?", "what lies on back, 1 hundred feet in air?", "where fortune tellers dance?", "what can hold without ever touching it?", "how can 4 suits dollar?"}; randomnumber = (int) (math.random() * jokes.length); system.out.println("bot: " + jokes[randomnumber] + " type reveal view answer"); scanner input = new scanner(system.in); { system.out.print("you: "); userinput = input.nextline(); userinput = userinput.tolowercase(); if (userinput.equals("reveal")) { system.out.println("bot: " + jokesanswer[randomnumber] + "\nbot: want hear joke? :d yes or no?"); } else { system.out.println("bot: wrong! >:d"); } } while (!userinput.equals("no")); }
your logic little bit broken. need tell joke within do-while loop, else you'll ever tell once. need able handle situation repeatedly type other 'reveal', added loop case. it's while-loop opposed do-while because not need guaranteed happen @ all.
scanner input = new scanner(system.in); { randomnumber = (int) (math.random() * jokes.length); system.out.println("bot: " + jokes[randomnumber] + " type reveal view answer"); system.out.print("you: "); userinput = input.nextline(); userinput = userinput.tolowercase(); while (!userinput.equals("reveal")) { system.out.println("bot: wrong! >:d"); system.out.print("you: "); userinput = input.nextline(); userinput = userinput.tolowercase(); } system.out.println("bot: " + jokesanswer[randomnumber] + "\nbot: want hear joke? :d yes or no?"); system.out.print("you: "); userinput = input.nextline(); userinput = userinput.tolowercase(); } while (!userinput.equals("no"));
i move user input statements own function avoid code duplication.
Comments
Post a Comment