Last active
March 25, 2021 20:26
-
-
Save FrenchTechLead/baa544bbd1823b76587908a1868f4d63 to your computer and use it in GitHub Desktop.
Calculator V1
This file contains 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 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