Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TalkingCalculatorV2 {
- String userInput;
- String userName;
- Scanner myScanner = new Scanner(System.in);
- public static void main(String [] args) {
- TalkingCalculatorV2 calculator = new TalkingCalculatorV2();
- calculator.start();
- }
- public void start() {
- System.out.println("Hello, what is your name?");
- userInput = myScanner.next();
- userName = userInput;
- if(userName.equalsIgnoreCase("graystriped")) {
- System.out.println("OMG! You coded me!!!!!!!!!!!!! Welcome Mr. Coder!");
- }
- else {
- System.out.println(userName + ". That's a cool name!");
- }
- System.out.println("Do you want to do some math? Or just talk?");
- userInput = myScanner.next();
- if(userInput.equalsIgnoreCase("math")) {
- calculate();
- }
- else if(userInput.equalsIgnoreCase("talk")) {
- talk();
- }
- else {
- System.out.println("Hey " + userName + ". Why are you not answering me correctly? " + userInput + " doesn't answer my question!");
- whatNext();
- }
- }
- public void calculate() {
- double firstNum = 0;
- double secondNum = 0;
- System.out.println("Do you want to add, subtract, multiply, or divide?");
- userInput = myScanner.next();
- if(!userInput.equalsIgnoreCase("add") && !userInput.equalsIgnoreCase("subtract") && !userInput.equalsIgnoreCase("multiply") && !userInput.equalsIgnoreCase("divide")) {
- System.out.println(userInput + " doesn't answer my question. Be careful " + userName);
- calculate();
- }
- System.out.println("What do you want to " + userInput + "?");;
- System.out.print("First number: ");
- if(myScanner.hasNextDouble()) {
- firstNum = myScanner.nextDouble();
- }
- else {
- 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!");
- calculate();
- }
- System.out.println("\nGreat!");
- System.out.print("Now for the second number: ");
- if(myScanner.hasNextDouble()) {
- secondNum = myScanner.nextDouble();
- }
- else {
- 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!");;
- calculate();
- }
- System.out.println("\n");
- if(userInput.equalsIgnoreCase("add")) {
- double answer = firstNum + secondNum;
- System.out.println("Here is your answer: " + answer);
- }
- else if(userInput.equalsIgnoreCase("subtract")) {
- double answer = firstNum - secondNum;
- System.out.println("Here is your answer: " + answer);
- }
- else if(userInput.equalsIgnoreCase("multiply")) {
- double answer = firstNum * secondNum;
- System.out.println("Here is your answer: " + answer);
- }
- else if(userInput.equalsIgnoreCase("divide")) {
- double answer = firstNum / secondNum;
- System.out.println("Here is your answer: " + answer);
- }
- whatNext();
- }
- public void talk() {
- System.out.println("So, what is your favorite color?");
- userInput = myScanner.next();
- if(userInput.equalsIgnoreCase("green")) {
- System.out.println("Really? Cool! Me too!");
- }
- else {
- System.out.println(userInput + "? My favorite color is better! My favorite color is green!");
- }
- System.out.println("What is your favorite animal?");
- userInput = myScanner.next();
- if(userInput.equalsIgnoreCase("shark")) {
- System.out.println("Cool! Me too!");
- }
- else if(userInput.equalsIgnoreCase("fish")) {
- System.out.println("Fish are cool too, they're related to my favorite animal, the shark.");
- }
- else if(userInput.equalsIgnoreCase("cat")) {
- System.out.println("A cat? Seriously? I like sharks, they would eat your cat.");
- }
- else {
- System.out.println(userInput + " are cool too, but I prefer sharks.");
- }
- long guess = 0;
- int randomNumber = (int)(Math.random()*10);
- 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!");
- if(myScanner.hasNextDouble()) {
- guess = (long) myScanner.nextDouble();
- if(guess == randomNumber) {
- System.out.println("That's correct!");
- }
- else {
- System.out.println("Sorry, that's not it, try again some other time.");
- System.out.println("The number I was thinking of was " + randomNumber);
- }
- }
- else {
- System.out.println("You tried to kill me! That wasn't a number!");
- myScanner.next();
- }
- whatNext();
- }
- public void whatNext() {
- System.out.println("\n\nWhat do you want to do? Math? Talk? Or exit?");
- userInput = myScanner.next();
- if(userInput.equalsIgnoreCase("math")) {
- calculate();
- }
- else if(userInput.equalsIgnoreCase("talk")) {
- talk();
- }
- else if(userInput.equalsIgnoreCase("exit")) {
- System.exit(1);
- }
- else {
- System.out.println(userName + "!!!! Why are you trying to confuse me??? " + userInput + " is not a choice!");
- whatNext();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement