Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Day14A {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- double num1, num2;
- String storeValue, opr;
- try {
- System.out.print("Enter 1st value: ");
- storeValue = sc.nextLine();
- num1 = Double.parseDouble(storeValue);
- System.out.print("Enter math operation(+ - * /): ");
- opr = sc.nextLine();
- System.out.print("Enter 2nd value: ");
- storeValue = sc.nextLine();
- num2 = Double.parseDouble(storeValue);
- compute(num1, num2, opr);
- } catch (Exception e) {
- System.out.println("Error on the user typed value");
- } finally {
- System.out.println("code complete");
- }
- }
- // modified code from Day10A and Day12F file
- static void compute(double n1, double n2, String op){
- boolean computeSuccess = true;
- String printout ="";
- double result = 0;
- switch (op) {
- case "+":
- result = n1 + n2;
- break;
- case "-":
- result = n1 - n2;
- break;
- case "*":
- result = n1 * n2;
- break;
- case "/":
- try {
- result = n1 / n2;
- } catch (Exception e) {
- printout = "Cannot divide by zero";
- computeSuccess = false;
- }
- break;
- default:
- printout = "Operator not on list";
- computeSuccess = false;
- }
- if (computeSuccess) {
- printout = String.format("%.2f %s %.2f = %.4f"
- , n1, op, n2, result);
- }
- System.out.println(printout);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment