SHARE
TWEET

Untitled

a guest Oct 14th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * CS 149 Exercise 6.3 Magic 8-Ball.
  5.  *
  6.  * @author Jordan Brink
  7.  * @version 10/14/19
  8.  */
  9. public class EightBall {
  10.  
  11.     /**
  12.      * Displays the given prompt, followed by "[yes/no]: ". Returns true if the
  13.      * next line of user input is "y" or "yes" ignoring case, false otherwise.
  14.      *
  15.      * @param in the Scanner to use
  16.      * @param prompt the text to display
  17.      * @return true if yes, false if no
  18.      */
  19.     public static boolean inputYesNo(Scanner in, String prompt) {
  20.    
  21.         System.out.print(prompt + "[yes/no]: ");
  22.         String input = in.nextLine();
  23.         return input.equalsIgnoreCase("y") || input.equalsIgnoreCase("yes");
  24.     }
  25.  
  26.     /**
  27.      * Gets the next question from the user. The length of the question must be
  28.      * between 1 and 60 characters, and the question must end with a '?'. This
  29.      * method keeps asking the user for a question until they enter a valid one.
  30.      *
  31.      * @param in the Scanner to use
  32.      * @param prompt the text to display
  33.      * @return the user's question
  34.      */
  35.     public static String getQuestion(Scanner in, String prompt) {    
  36.         System.out.print(prompt);
  37.         String input = in.nextLine();
  38.            
  39.             while (input.length() >= 60) {
  40.                 System.out.println("Your question is too long");
  41.                 System.out.print(prompt);
  42.                 input = in.nextLine();
  43.             }
  44.            
  45.             while (!(input.endsWith("?"))) {
  46.                 System.out.println("Your question must end with a ?");
  47.                 System.out.print(prompt);
  48.                 input = in.nextLine();
  49.             }
  50.            
  51.             while (input.equals("")) {
  52.                 System.out.println("Your question is blank");
  53.                 System.out.println(prompt);
  54.                 input = in.nextLine();
  55.         }  
  56.        
  57.         return input;      
  58.     }
  59.  
  60.     /**
  61.      * Simulate a Magic 8-Ball. Original code by Arch Harris and Nancy Harris.
  62.      *
  63.      * @param args command-line arguments
  64.      */
  65.     public static void main(String[] args) {
  66.         Scanner in = new Scanner(System.in);
  67.         System.out.println("Magic 8-Ball");
  68.  
  69.         // run the program as long as the user wants
  70.         while (inputYesNo(in, "\nDo you want to ask a question? ")) {
  71.  
  72.             // get the next question
  73.             String question = getQuestion(in, "What is your question? ");
  74.             System.out.println();
  75.  
  76.             // pick a random answer
  77.             String answer;
  78.             int randInt = (int) (Math.random() * 20 + 1);
  79.             switch (randInt) {
  80.                 case 1:
  81.                     answer = "Signs point to yes.";
  82.                     break;
  83.                 case 2:
  84.                     answer = "Yes.";
  85.                     break;
  86.                 case 3:
  87.                     answer = "Reply hazy, try again.";
  88.                     break;
  89.                 case 4:
  90.                     answer = "Without a doubt.";
  91.                     break;
  92.                 case 5:
  93.                     answer = "My sources say no.";
  94.                     break;
  95.                 case 6:
  96.                     answer = "As I see it, yes.";
  97.                     break;
  98.                 case 7:
  99.                     answer = "You may rely on it.";
  100.                     break;
  101.                 case 8:
  102.                     answer = "Concentrate and ask again.";
  103.                     break;
  104.                 case 9:
  105.                     answer = "Outlook not so good.";
  106.                     break;
  107.                 case 10:
  108.                     answer = "It is decidedly so.";
  109.                     break;
  110.                 case 11:
  111.                     answer = "Better not tell you now.";
  112.                     break;
  113.                 case 12:
  114.                     answer = "Very doubtful.";
  115.                     break;
  116.                 case 13:
  117.                     answer = "Yes - definitely.";
  118.                     break;
  119.                 case 14:
  120.                     answer = "It is certain.";
  121.                     break;
  122.                 case 15:
  123.                     answer = "Cannot predict now.";
  124.                     break;
  125.                 case 16:
  126.                     answer = "Most likely.";
  127.                     break;
  128.                 case 17:
  129.                     answer = "Ask again later.";
  130.                     break;
  131.                 case 18:
  132.                     answer = "My reply is no.";
  133.                     break;
  134.                 case 19:
  135.                     answer = "Outlook good.";
  136.                     break;
  137.                 case 20:
  138.                     answer = "Don't count on it.";
  139.                     break;
  140.                 default:
  141.                     answer = "HUH?";
  142.                     break;
  143.             }
  144.  
  145.             // output the results
  146.             System.out.printf("Question: %s\n", question);
  147.             System.out.printf("  Answer: %s\n", answer);
  148.         }
  149.         System.out.println("Goodbye!");
  150.     }
  151.  
  152. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top