java programming Create a program named Magic8Ball In the main method, create a
ID: 3875853 • Letter: J
Question
java programming
Create a program named Magic8Ball In the main method, create a loop that continually asks the user to type a question. If the user enters a blank question ("), exit the loop. Otherwise, call a method named getAnswer which returns a String containing the answer to the user's question. Print that answer and prompt for another question. In the getAnswer method, generate a random integer in the range of 1 to 10. Using if statements, return one of ten possible answers corresponding to the random number. You can make the answer text. Sample user entered value 8 SAMPLE OUTPUT: $ java Magic8Bal.1 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: Will 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:Explanation / Answer
Magic8Ball.java
import java.util.Random;
import java.util.Scanner;
public class Magic8Ball {
public static void main(String[] args) {
//Declaring variables
String ques;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.println("I AM THE MAGIC 8 BALL");
//Read the Question entered by the user
System.out.print("Ask me a question abouty the future :");
ques = sc.nextLine();
//This loop continue to execute until the Question is Empty
while (!ques.isEmpty()) {
//Calling the method
String answer = getAnswer();
//Displaying the Answer
System.out.println(answer);
System.out.print("Ask me a question abouty the future :");
ques = sc.nextLine();
}
}
//This method will randomly generate the answer and return to the caller
private static String getAnswer() {
String ans = null;
//Creating a random Class object
Random r = new Random();
int num = r.nextInt((10 - 1) + 1) + 1;
if (num == 1) {
ans = "Cannot predict now";
} else if (num == 2) {
ans = "Concentrate and ask again";
} else if (num == 3) {
ans = "Outlook no so good";
} else if (num == 4) {
ans = "It will be good";
} else if (num == 5) {
ans = "Do your work.Rest leave to god";
} else if (num == 6) {
ans = "You will be happy";
} else if (num == 7) {
ans = "Everything will be alright";
} else if (num == 8) {
ans = "From here your life will be good";
} else if (num == 9) {
ans = "Be happy";
} else if (num == 10) {
ans = "Have fun";
}
return ans;
}
}
___________________
Output:
I AM THE MAGIC 8 BALL
Ask me a question abouty the future :What is the meaning of life?
Everything will be alright
Ask me a question abouty the future :Will I have great ritches?
Concentrate and ask again
Ask me a question abouty the future :Will I become wealthy?
Be happy
Ask me a question abouty the future :
_______________Could you plz rate me well.Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.