Skip to content

Instantly share code, notes, and snippets.

@DrBluefall
Last active July 11, 2019 00:49
Show Gist options
  • Save DrBluefall/bbc4991335a6e5da8135f33fdbc7dbea to your computer and use it in GitHub Desktop.
Save DrBluefall/bbc4991335a6e5da8135f33fdbc7dbea to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Welcome to the calculator! There are 4 functions: \n" + " - add \n" + " - subtract \n"
+ " - multply \n" + " - divide \n" + " \n " + "Input the operation you wish to perform, or type 'quit' to quit: ");
String input = in.nextLine();
Calculator op = new Calculator();
if (input.equals("add")) {
double[] values = op.getValues();
double result = op.add(values[0], values[1]);
System.out.println(result);
} else if(input.equals("subtract")) {
double[] values = op.getValues();
double result = op.subtract(values[0], values[1]);
System.out.println(result);
} else if(input.equals("multiply")) {
double[] values = op.getValues();
double result = op.multiply(values[0], values[1]);
System.out.println(result);
} else if(input.equals("divide")) {
double[] values = op.getValues();
double result = op.divide(values[0], values[1]);
System.out.println(result);
}
in.close();
}
public double add(double num1, double num2) {
double result = num1 + num2;
return result;
}
public double subtract(double num1, double num2) {
double result = num1 - num2;
return result;
}
public double multiply(double num1, double num2) {
double result = num1 * num2;
return result;
}
public double divide(double num1, double num2) {
double result = num1 / num2;
return result;
}
public double[] getValues() {
Scanner in = new Scanner(System.in);
System.out.print("Input value 1: ");
double num1 = in.nextDouble();
System.out.print("Input value 2: ");
double num2 = in.nextDouble();
double input[] = {num1 , num2};
in.close();
return input;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment