Magic 8 Ball is a toy developed in the 1950s and used for fortune-telling or advice-seeking. A player asks or thinks of a yes-or-no question such as, Will I be rich someday? The player then turns the ball over to see one of 20 randomly chosen responses, for example, It is certain or Very doubtful. Create a program that simulates the toy by allowing a user to type a question; the user then receives one of 20 random responses. Allow the user to keep asking questions until the user types in a sentinel value. Save the program as Magic8Ball.java.
import java.util.Random;
public class Magic8Ball
{
public static void main( String[] args )
{
Random r = new Random();
int choice = 1 + r.nextInt(20);
String response = "";
if ( choice == 1 )
response = "Hi Hello";
else if ( choice == 2 )
response = "How are you all";
else if ( choice == 3 )
response = "welcome to the session";
else if ( choice == 4 )
response = "friends and colleagues";
else if ( choice == 5 )
response = "You may rely on it";
else if ( choice == 6 )
response = "yes u can count on me";
else if ( choice == 7 )
response = "hundred percent sure";
else if ( choice == 8 )
response = "good work in college";
else if ( choice == 9 )
response = "I may get a possible outcome";
else if ( choice == 10 )
response = "Ya sure";
else if ( choice == 11 )
response = " but it says,,Reply hazy, try again";
else if ( choice == 12 )
response = "Ask again later";
else if ( choice == 13 )
response = "Better not to tell anyone";
else if ( choice == 14 )
response = "Cannot predict";
else if ( choice == 15 )
response = "leave the college";
else if ( choice == 16 )
response = "ask everyone";
else if ( choice == 17 )
response = "My reply is no";
else if ( choice == 18 )
response = "My sources say no";
else if ( choice == 19 )
response = "output is a problem";
else if ( choice == 20 )
response = "successful completion of program";
else
response = "8-BALL ERROR!";
System.out.println( "MAGIC 8-BALL SAYS: " + response );
}
}
Get Answers For Free
Most questions answered within 1 hours.