I am trying to create a Magic 8 Ball program that:
Stores all the responses in a String array using an initializer list.
Generates a random number for the responses using a random object (in the range 0 to 19).
Prompts the user to enter a question.
Uses the random number to access and display the corresponding response.
Asks the user if they want to ask another question (using a do-while loop). The code should repeat as long as the user says “yes”.
I am having trouble with step 5.
All the other steps I have been able to accomplish well, but when I input "yes", instead of it letting me ask another question it just skips that and gives me the answer.
Here is what I have so far:
//initializing variables
int stop = 0;
String otherQ,q;
//initializing array
String[] responses = {
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful."};
//creates objects
Scanner scan = new Scanner (System.in);
Random rn = new Random();
//input
//THIS IS WHERE I AM HAVING A PROBLEM.
do {
System.out.print("What is your question? ");
q = scan.nextLine();
System.out.println(responses[rn.nextInt(19)]); //method caller
while (stop == 0) {
System.out.print("Would you like to ask another question? (Answer yes or no): ");
otherQ = scan.next();
if (otherQ.equalsIgnoreCase("yes")){
break;
}else if (otherQ.equalsIgnoreCase("no")){
stop = 1;
}
}
} while (stop == 0);
My expected result is:
What is your question? Question goes here
As I see it, yes.
Would you like to ask another question? (Answer yes or no): yes
What is your question? Cannot predict now.
Would you like to ask another question? (Answer yes or no):
The results I am getting with the code above:
What is your question? Question goes here
It is certain.
Would you like to ask another question? (Answer yes or no): yes
What is your question? Question goes here
Would you like to ask another question? (Answer yes or no): no
THANK YOU SO MUCH FOR HELPING ME!
A correct implementation for this question would be as follows:
//initializing variables
int stop = 0;
String otherQ,q;
//initializing array
String[] responses = {
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful."};
//creates objects
Scanner scan = new Scanner (System.in);
Random rn = new Random();
//input
//THIS IS WHERE I AM HAVING A PROBLEM.
do {
System.out.print("What is your question? ");
q = scan.nextLine();
System.out.println(responses[rn.nextInt(19)]); //method caller
System.out.print("Would you like to ask another question? (Answer yes or no): ");
otherQ = scan.nextLine();
} while (otherQ.equalsIgnoreCase("yes"));
You can drop the nested while loop in the do-while, remember a do-while
loop only needs the one condition at the end of the do
section.
Your logic was in the right direction, get the user's question, get the answer, then ask them if they want to ask another question.
Also, swap the .next()
to a .nextLine()
for getting the user's decision to continue.
I just made another small update at the bottom to avoid the confusing conditionals you added so that yes = 1
and no = 0
.