In: Computer Science
Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number and then use a switch statement to display the message.
Use this website as a resource on possible answers a Magic Eight Ball gives: https://en.wikipedia.org/wiki/Magic_8-Ball
Magic8_Ball.java:
import java.io.*;
import java.util.*;
import java.util.Random;
import java.lang.*;
public class Magic8_Ball
{
public static void main(String[]
args)
{
//generating
random number using Math.Random()
int random_magic
= (int) Math.ceil(Math.random() *20);
//aanswer
variable for storing and displaying response
String answer =
"";
//switch case
for different output cases
switch(random_magic)
{
case 1:
answer = "It is
certain";
break;
case 2:
answer = "It is decidedly
so";
break;
case 3:
answer = "Without a
doubt";
break;
case 4:
answer = "Yes -
definitely";
break;
case 5:
answer = "You may rely on
it";
break;
case 6:
answer = "As i see it,
yes";
break;
case 7:
answer = "Most likely";
break;
case 8:
answer = "Outlook
good";
break;
case 9:
answer = "Yes";
break;
case 10:
answer = "Signs point to
yes";
break;
case 11:
answer = "Reply hazy, try
again";
break;
case 12:
answer = "Ask again
later";
break;
case 13:
answer = "Better not tell you
now";
break;
case 14:
answer = "Cannot predict
now";
break;
case 15:
answer = "Concentrate ans ask
again";
break;
case 16:
answer = "Don;t count on
it";
break;
case 17:
answer = "My reply is
no";
break;
case 18:
answer = "My sources say
no";
break;
case 19:
answer = "Outlook not so
good";
break;
case 20:
answer = "Very
doubtful";
break;
default:
answer = "8-Ball
Error";
break;
}
//printing the
response
System.out.println("\nMagic 8-Ball says: " + answer);
}
}
Output: