Advertisement
Graystriped

Talking Calculator Source Code (V2)

May 14th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TalkingCalculatorV2 {
  4.     String userInput;
  5.     String userName;
  6.  
  7.     Scanner myScanner = new Scanner(System.in);
  8.  
  9.     public static void main(String [] args) {
  10.       TalkingCalculatorV2 calculator = new TalkingCalculatorV2();
  11.       calculator.start();
  12.     }
  13.  
  14.  
  15.  
  16.     public void start() {
  17.       System.out.println("Hello, what is your name?");
  18.  
  19.       userInput = myScanner.next();
  20.       userName = userInput;
  21.      
  22.       if(userName.equalsIgnoreCase("graystriped")) {
  23.           System.out.println("OMG! You coded me!!!!!!!!!!!!! Welcome Mr. Coder!");
  24.       }
  25.       else {
  26.           System.out.println(userName + ". That's a cool name!");
  27.       }
  28.  
  29.       System.out.println("Do you want to do some math? Or just talk?");
  30.  
  31.       userInput = myScanner.next();
  32.  
  33.       if(userInput.equalsIgnoreCase("math")) {
  34.         calculate();
  35.       }
  36.  
  37.       else if(userInput.equalsIgnoreCase("talk")) {
  38.         talk();
  39.       }
  40.  
  41.       else {
  42.         System.out.println("Hey " + userName + ". Why are you not answering me correctly? " + userInput + " doesn't answer my question!");
  43.  
  44.  
  45.         whatNext();
  46.       }
  47.     }
  48.  
  49.  
  50.  
  51.     public void calculate() {
  52.       double firstNum = 0;
  53.       double secondNum = 0;
  54.  
  55.  
  56.       System.out.println("Do you want to add, subtract, multiply, or divide?");
  57.  
  58.       userInput = myScanner.next();
  59.  
  60.  
  61.       if(!userInput.equalsIgnoreCase("add") && !userInput.equalsIgnoreCase("subtract") && !userInput.equalsIgnoreCase("multiply") && !userInput.equalsIgnoreCase("divide")) {
  62.         System.out.println(userInput + " doesn't answer my question. Be careful " + userName);
  63.  
  64.         calculate();
  65.       }
  66.  
  67.       System.out.println("What do you want to " + userInput + "?");;
  68.  
  69.  
  70.       System.out.print("First number: ");
  71.  
  72.       if(myScanner.hasNextDouble()) {
  73.         firstNum = myScanner.nextDouble();
  74.  
  75.       }
  76.  
  77.  
  78.       else {
  79.         System.out.println(userName + ", do you hate me? If I didn't catch that it could've killed me! " + myScanner.next() + " is not a number!");
  80.  
  81.  
  82.         calculate();      
  83.        
  84.         }
  85.        
  86.  
  87.       System.out.println("\nGreat!");
  88.  
  89.       System.out.print("Now for the second number: ");
  90.  
  91.  
  92.  
  93.       if(myScanner.hasNextDouble()) {
  94.         secondNum = myScanner.nextDouble();
  95.       }
  96.  
  97.       else {
  98.         System.out.println(userName + ", do you hate me? If I didn't catch that it could've killed me!" + myScanner.next() + " is not a number!");;
  99.  
  100.         calculate();
  101.       }
  102.  
  103.  
  104.       System.out.println("\n");
  105.  
  106.  
  107.       if(userInput.equalsIgnoreCase("add")) {
  108.         double answer = firstNum + secondNum;
  109.         System.out.println("Here is your answer: " + answer);
  110.  
  111.       }
  112.  
  113.  
  114.       else if(userInput.equalsIgnoreCase("subtract")) {
  115.         double answer = firstNum - secondNum;
  116.         System.out.println("Here is your answer: " + answer);
  117.  
  118.       }
  119.  
  120.       else if(userInput.equalsIgnoreCase("multiply")) {
  121.           double answer = firstNum * secondNum;
  122.         System.out.println("Here is your answer: " + answer);
  123.       }
  124.  
  125.       else if(userInput.equalsIgnoreCase("divide")) {
  126.           double answer = firstNum / secondNum;
  127.         System.out.println("Here is your answer: " + answer);
  128.  
  129.       }
  130.  
  131.       whatNext();
  132.     }
  133.  
  134.  
  135.  
  136.     public void talk() {
  137.       System.out.println("So, what is your favorite color?");
  138.  
  139.       userInput = myScanner.next();
  140.  
  141.  
  142.       if(userInput.equalsIgnoreCase("green")) {
  143.         System.out.println("Really? Cool! Me too!");
  144.       }
  145.  
  146.       else {
  147.         System.out.println(userInput + "? My favorite color is better! My favorite color is green!");
  148.       }
  149.  
  150.       System.out.println("What is your favorite animal?");
  151.  
  152.       userInput = myScanner.next();
  153.  
  154.  
  155.       if(userInput.equalsIgnoreCase("shark")) {
  156.         System.out.println("Cool! Me too!");
  157.  
  158.  
  159.       }
  160.  
  161.       else if(userInput.equalsIgnoreCase("fish")) {
  162.         System.out.println("Fish are cool too, they're related to my favorite animal, the shark.");
  163.       }
  164.  
  165.       else if(userInput.equalsIgnoreCase("cat")) {
  166.         System.out.println("A cat? Seriously? I like sharks, they would eat your cat.");
  167.       }
  168.  
  169.       else {
  170.         System.out.println(userInput + " are cool too, but I prefer sharks.");
  171.       }
  172.  
  173.       long guess = 0;
  174.  
  175.       int randomNumber = (int)(Math.random()*10);
  176.  
  177.  
  178.       System.out.println("I have a number between 0 and 10 in my mind. Can you find it out? If you enter anything that isn't a number I will die!");
  179.  
  180.  
  181.       if(myScanner.hasNextDouble()) {
  182.         guess = (long) myScanner.nextDouble();
  183.  
  184.         if(guess == randomNumber) {
  185.           System.out.println("That's correct!");
  186.  
  187.       }
  188.  
  189.       else {
  190.         System.out.println("Sorry, that's not it, try again some other time.");
  191.  
  192.         System.out.println("The number I was thinking of was " + randomNumber);
  193.       }
  194.       }
  195.  
  196.       else {
  197.         System.out.println("You tried to kill me! That wasn't a number!");
  198.  
  199.         myScanner.next();
  200.  
  201.       }
  202.  
  203.       whatNext();
  204.     }
  205.  
  206.  
  207.  
  208.     public void whatNext() {
  209.       System.out.println("\n\nWhat do you want to do? Math? Talk? Or exit?");
  210.  
  211.       userInput = myScanner.next();
  212.  
  213.       if(userInput.equalsIgnoreCase("math")) {
  214.         calculate();
  215.       }
  216.  
  217.       else if(userInput.equalsIgnoreCase("talk")) {
  218.         talk();
  219.       }
  220.  
  221.       else if(userInput.equalsIgnoreCase("exit")) {
  222.         System.exit(1);
  223.  
  224.       }
  225.  
  226.       else {
  227.         System.out.println(userName + "!!!! Why are you trying to confuse me??? " + userInput + " is not a choice!");
  228.  
  229.         whatNext();
  230.       }
  231.     }
  232.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement