Created
June 3, 2018 18:54
-
-
Save ashikuzzaman-ar/03cb04c876c11d14375f43a98a5ba80b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.studevs.example.test.core; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Please type your name: "); | |
String name = scanner.nextLine(); | |
try { | |
System.out.println("Hello " + name + ", please follow to rules to use the calculator."); | |
System.out.println("To add two number input like, 12 + 2.4"); | |
System.out.println("To subtract two number input like, 12 - 2.4"); | |
System.out.println("To multiply two number input like, 12 * 2.4"); | |
System.out.println("To divide first number by second number input like, 12 / 2.4"); | |
System.out.println("Be casefult to give space between first number, operator and second number!"); | |
System.out.println("Use EXIT or exit to terminate the program!\n\n\n"); | |
while (true) { | |
String firstNumer = scanner.next(); | |
if (firstNumer.trim().toUpperCase().equals("EXIT")) { | |
System.out.println("Thank you " + name + " to use this calculator!"); | |
break; | |
} | |
String operator = scanner.next(); | |
String secondNumber = scanner.next(); | |
switch (operator) { | |
case "+": { | |
System.out.println(Double.valueOf(firstNumer) + Double.valueOf(secondNumber)); | |
break; | |
} | |
case "-": { | |
System.out.println(Double.valueOf(firstNumer) - Double.valueOf(secondNumber)); | |
break; | |
} | |
case "*": { | |
System.out.println(Double.valueOf(firstNumer) * Double.valueOf(secondNumber)); | |
break; | |
} | |
case "/": { | |
System.out.println(Double.valueOf(firstNumer) / Double.valueOf(secondNumber)); | |
break; | |
} | |
default: | |
System.out.println("Invalid input type!"); | |
break; | |
} | |
System.out.println("Try again."); | |
} | |
} catch (Exception e) { | |
System.out.println("You have done a serious mistake!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment