Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int calculate(String input){
- int lhs,rhs; //short for left-hand & right-hand side
- char operation;
- Scanner scanner = new Scanner(input);
- lhs = scanner.nextInt();
- while(scanner.hasNext()) {
- operation = scanner.next().charAt(0);
- rhs = scanner.nextInt();
- switch(operation) {
- case '+':
- lhs += rhs;
- break;
- case '-':
- lhs -= rhs;
- break;
- case '*':
- lhs *= rhs;
- break;
- case '/':
- lhs /= rhs;
- break;
- default:
- //error!! wrong operation, do something
- break;
- }
- System.out.println(lhs);
- }
- /*todo: your name and code goes here*/
- /*You need a Scanner(or StringTokenizer) to get tokens
- *Then you need a loop, and switch inside that loop*/
- return lhs;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement