Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.21 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner; // Imports the scanner for keyboard input.
  3.  
  4. //Name: EightBall
  5. //Author: Matt Lynch
  6.  
  7. public class eightball { // Declares variables outside the public static void function so it can used anywhere.
  8.    
  9.     String name;
  10.     int answer;
  11.     int a;
  12.     int b;
  13.     int c;
  14.     int dicea;
  15.     int diceb;
  16.     int dicec;
  17.     String[] eightanswer = {"Test", "It is certain", "It is decidedly so.", "Without a doubt.", "Yes, definitely.", "You may rely on it.", "As I see it, yes.", "Most likely.", "Outlook good.", "Yes.", "Signs point to yes.", "Reply hazy, try again.", "Ask again later.", "Better not tell you now.", "Cannot predict now.", "Concentrate and ask again!", "Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful.", "It is foretold in the scrolls.", "No.", "Don't press your luck.", "Luck is on your side,", "Maybe." }; //All the answers the 8ball has, the first answer "test" cannot be seen.
  18.     String[] coin = {"Heads", "Tails"}; //Array for the coin game
  19.     int[] dice = {1, 2, 3, 4, 5, 6}; //Array for the dice game
  20.            
  21.     Random r = new Random(); // Starts up the random generator.
  22.     Scanner keyboard = new Scanner(System.in); // Starts up the keyboard input.
  23.  
  24.     public static void main(String[] args) // Main function
  25.     {
  26.         new eightball(); // This creates a new instance of the class which runs the code inside of the constructor.
  27.     }
  28.    
  29.     public eightball() // Starts a constructor. A Constructor is a function which ALWAYS starts when a new object of the class is created.
  30.     {
  31.         start(); // Runs the program, when start(); is initialised again, it reverts back to start.
  32.     }
  33.    
  34.     void start() // This is what start(); reverts back to.
  35.     {
  36.         int x = 0 + r.nextInt(24);
  37.        
  38.         System.out.println("+---------------------------------------------+");
  39.         System.out.println("| Hello! Welcome to the 8-Ball Program!       |"); // This is a title screen that shows the user what the program is and give the author's name.
  40.         System.out.println("| \t\tBy Matt Lynch                 |");
  41.         System.out.println("+---------------------------------------------+");
  42.         System.out.println(); // Line break.
  43.         System.out.println("What is your name?");
  44.         System.out.print("> "); //Simple symbol to show input.
  45.         name = keyboard.next(); // Gives the command to make the user type out their name which is stored in "String name;".
  46.         System.out.println(); // Line break.
  47.         System.out.println("Hello " + name + "! Here are a list of options you can do!"); // Another line of text.
  48.         System.out.println("1. 8-Ball!"); // If the user types 1 after this, they are given an 8-ball.
  49.         System.out.println("2. Flip a coin!"); // If the user types 2 after this, they can flip a coin.
  50.         System.out.println("3. Roll a dice!"); // If the user types 3 after this, they can roll a dice.
  51.         System.out.println("4. Quit"); // If the user types 4 after this, the program quits.
  52.         System.out.println(); // Line break.
  53.         System.out.println("Please, choose a number: "); // Asks the user to input a number.
  54.         System.out.print("> "); //Simple symbol to show input.
  55.         answer = keyboard.nextInt(); // The user inputs a number between 1 to 4.
  56.        
  57.         if (answer == 1) // If the user inputs 1.
  58.         {
  59.             System.out.println(); // Line Break.
  60.             System.out.println("Welcome to the 8-Ball function of the program. This program is fit with 25 answers. Please, ask a question."); // Asks the user to ask a question.
  61.             System.out.print("> "); //Simple symbol to show input.
  62.             String question = keyboard.nextLine(); // The user inputs a question.
  63.             System.out.println(); // Line Break.
  64.             System.out.println("Your question: " + question); // What the question is
  65.             System.out.println("The 8-Ball's answer: " + eightanswer[x]); // The answer
  66.             System.out.println(); // Line Break.
  67.             System.out.println("Would you like to ask another? [1] for yes! [2] for no!"); // Asks the user if they would like to ask another question
  68.             System.out.print("> "); //Simple symbol to show input.
  69.             int anotheranswer = keyboard.nextInt(); // The user inputs 1 or 2.
  70.                 while ( anotheranswer != 2 ) // While the input from above is not 2.
  71.                 {
  72.                     x = 0 + r.nextInt(24);
  73.                    
  74.                     System.out.println(); // Line Break.
  75.                     System.out.println("Ask a question."); // Asks the user to ask a question.
  76.                     System.out.print("> "); //Simple symbol to show input.
  77.                     question = keyboard.nextLine();
  78.                     System.out.println("Your question: " + question); // What the question is
  79.                     System.out.println("The 8-Ball's answer: " + eightanswer[x]); // The answer
  80.                     System.out.println(); // Line Break.
  81.                     System.out.println("Would you like to ask another? [1] for yes! [2] for no!"); // Asks the user if they would like to ask another question
  82.                     System.out.print("> "); //Simple symbol to show input.
  83.                     anotheranswer = keyboard.nextInt(); // The user inputs 1 or 2.
  84.                    
  85.                 }
  86.             System.out.println("Returning back to start!"); //Returning back to start
  87.             start(); //Returns back to start
  88.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement