Skip to content

Instantly share code, notes, and snippets.

@FrenchTechLead
Last active March 25, 2021 20:26
Show Gist options
  • Save FrenchTechLead/baa544bbd1823b76587908a1868f4d63 to your computer and use it in GitHub Desktop.
Save FrenchTechLead/baa544bbd1823b76587908a1868f4d63 to your computer and use it in GitHub Desktop.
Calculator V1
package meshredded;
import java.util.ArrayList;
import java.util.List;
class Calculator {
public static void main(String[] args) {
List<String> entries = new ArrayList<>();
entries.add("3 + 5");
entries.add("4 - 1");
entries.add("6 / 2");
entries.add("3 * 2");
entries.forEach(entry -> {
String[] splited = entry.split("\\+|\\-|\\/|\\*");
int left = new Integer(splited[0].trim());
int right = new Integer(splited[1].trim());
String symbol = entry.replaceAll("[0-9]|\\s", "");
int result = 0;
switch (symbol) {
case "+":
result = left + right;
break;
case "-":
result = left - right;
break;
case "*":
result = left * right;
break;
case "/":
result = left / right;
break;
}
System.out.println(entry + " = " + result);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment