In: Computer Science
Magic 8 Ball(JAVA)
A magic 8 ball is a popular fortune telling toy in which the user
places the ball face down, asks a
yes-or-no question and turns the ball face up to reveal the
answer.
The standard magic 8 ball has 20 standard answers shown below,
where 10 are positive (green),
5 are non-committal (yellow), and 5 are negative (red) (credit:
Wikipedia)
Write a program that does the following:
1. Stores all the responses in a String array using an initializer
list.
2. Generates a random number for the responses using a random
object (in the range 0 to 19)
3. Prompts the user to enter a question.
4. Uses the random number to access and display the corresponding
response (example: if the
random number generates 4 it should display “You may rely on
it.”)
5. 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”.
A sample of the output are shown below:
What is your question? Will John get me some Panera tomorrow?
Reply hazy, try again.
Would you like to ask another question? (Answer yes or no):
yes
What is your question? Will my dog become an instagram star?
Yes.
Would you like to ask another question? (Answer yes or no):
yes
What is your question? Will I pass this class if I study
hard?
Without a doubt.
Would you like to ask another question? (Answer yes or no):
no
Notes:
• Use the equalsIgnoreCase method to accept any version of the word
yes. You can use it
like str.equalsIgnoreCase(“yes”) where str is a variable of type
string.
• You may have to deal with the annoying problem of capturing the
[Enter] that is pressed
between responses. Remember, you can capture it with an extra
nextLine method – but
be sure to put it in the right place!
// Java program to implement the Magic 8 ball popular fortune
telling toy
import java.util.Random;
import java.util.Scanner;
public class Magic8Ball {
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
// array to store 20
repsonses
String responses[] = {"Yes.","Reply
hazy, try again.","It is certain.","No.","Most likely.","Without a
doubt.","Don't count on it.","You may rely on it.","It is decidedly
so.","Ask again later.","Yes definitely.","As I see it,
yes.","Signs point to yes.","Cannot predict now.","Concentrate and
ask again.","My sources say no.","Outlook not so good.","Very
doubtful.","Better not tell you now.","Outlook good."};
String question;
String contRes;
int index;
Random ran = new Random();
// loop that continues till the
user wats
do
{
// input the
question
System.out.print("What is your question? ");
question =
scan.nextLine();
// randomly
generate the index of response
index =
ran.nextInt(responses.length);
// display the
response
System.out.println(responses[index]);
// ask the user
if he wants to ask another question
System.out.print("Would you like to ask another question? (Answer
yes or no): ");
contRes =
scan.next(); // read the response
scan.nextLine();
// ignore the enter
}while(contRes.equalsIgnoreCase("yes"));
scan.close();
}
}
//end of program
Output: