public class Calculator { public static void main(String[] args) { try { int result = 0; try { switch (args[1].charAt(0)) { case '+': result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]); break; case '-': result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]); break; case '*': result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]); break; case '/': result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]); break; } System.out.println(args[0] + ' ' + args[1] + ' ' + args[2] + " = " + result); } catch(NumberFormatException ex) { System.out.println(ex.getMessage()); System.exit(0); } } catch(Exception ex) { System.out.println("You have to enter it in as: Number1 operator Number2"); System.exit(0); } } }
java Calculator 3 "*" 3 // or using x character java Calculator 3 x 3
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)