Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Last active April 15, 2020 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/ffc15f20aacef9851a31c2d6a6173dcc to your computer and use it in GitHub Desktop.
Save codecademydev/ffc15f20aacef9851a31c2d6a6173dcc to your computer and use it in GitHub Desktop.
Codecademy export
package magic8Ball;
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
public class magic8Ball {
String[] ballOutcome = {"Only solution for now" , "This is a second solution"};
public magic8Ball() {
}
public String getRandomNumber() { //Generate random number from 1 or 2
Random number = new Random();
int random = number.nextInt(2);
return "The random number is: " + random;
}
public String question() {
String input;
Scanner question = new Scanner(System.in);
input=question.nextLine();
return "Your question is: " + input;
}
public String getOutcomes() {
String[] outcome = ballOutcome;
Random dice = new Random();
int d = dice.nextInt(1);
System.out.println(outcome[d]);
return "The outcome is: " + outcome[d];
}
public static void main(String[] args) {
String answer;
Scanner scan = new Scanner(System.in);
magic8Ball ball = new magic8Ball();
for(int i = 0; i < 20; i++) {
System.out.println("Hello welcome to JMagic 8 Ball!");
System.out.println("");
System.out.println("Do you want to use the Magic 8 Ball?, type 'Yes', or 'No'");
answer=scan.next();
if(answer.equals("Yes")) {
System.out.println("Start by asking any question to the magic 8 ball:");
System.out.println(ball.question());
System.out.println("");
System.out.println("In 0.01 seconds you will receive a random number!");
System.out.println(ball.getRandomNumber());
System.out.println("Generating Magical Outcome:");
try {
// thread to sleep for 1000 milliseconds
Thread.sleep(7000);
} catch (Exception e) {
System.out.println(e);
}
System.out.println(ball.getOutcomes());
} else if(answer.equals("No")) {
System.out.println("Program will not continue");
} else {
System.out.println("Invalid");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment