Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

original Magic8Ball java program: import java.util.Random; import java.util.Scanner; public class Magic8Ball { public static void main(String[] args) { String s; Scanner sc = new Scanner(System.in);

image text in transcribed

original Magic8Ball java program:

import java.util.Random;

import java.util.Scanner;

public class Magic8Ball {

public static void main(String[] args) { String s; Scanner sc = new Scanner(System.in);

System.out.println("I AM THE MAGIC 8 BALL");

System.out.print("Ask me a question abouty the future :"); s = sc.nextLine();

while (!s.isEmpty()) {

String answer = getAnswer();

System.out.println(answer); System.out.print("Ask me a question abouty the future :"); s = sc.nextLine(); } }

public static String getAnswer() { String ans = null;

Random r = new Random(); int num = r.nextInt((10 - 1) + 1) + 1;

if (num == 1) { ans = "Cannot predict now"; } else if (num == 2) { ans = "Big ballin"; } else if (num == 3) { ans = "Outlook no so good"; } else if (num == 4) { ans = "Yes it will happen"; } else if (num == 5) { ans = "Stay in your lane"; } else if (num == 6) { ans = "don't ask again"; } else if (num == 7) { ans = "dilly dilly"; } else if (num == 8) { ans = "Yes sir"; } else if (num == 9) { ans = "Be happy"; } else if (num == 10) { ans = "Have fun"; } return ans; }

}

Rewrite your Magic8Ball program from Lab 9, using an array of Strings containing the answers instead of multiple if statements. Generate a random number to use as an index number to retrieve a String from your answers array. Continue to prompt for questions until the user presses enter without asking a question 8 SAMPLE OUTPUT: $ java Magic8Ball I AM THE MAGIC 8 BALL Ask me a question about your future: What is the meaning of life? Cannot predict now Ask me a question about your future: Wil1 I have great riches? Concentrate and ask again Ask me a question about your future:will I become wealthy? Outlook not so good Ask me a question about your future: Sample user entered value

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions