SHARE
TWEET

Untitled

a guest May 24th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class CalculatorParser {
  2.     public static int evaluate(String text) {
  3.         int result;
  4.        
  5.         String [] tab = text.split( " " );
  6.         int firstNumber = Integer.parseInt( tab[0] );
  7.         int secondNumber = Integer.parseInt( tab[2] );
  8.        
  9.         if(tab[1].equals( "+" ))
  10.         {
  11.             result = firstNumber + secondNumber;
  12.         }
  13.        
  14.         else if(tab[1].equals( "-" ))
  15.         {
  16.             result = firstNumber - secondNumber;
  17.         }
  18.        
  19.         else if(tab[1].equals( "*" ))
  20.         {
  21.             result = firstNumber * secondNumber;
  22.         }
  23.        
  24.         else
  25.         {
  26.             result = firstNumber / secondNumber;
  27.         }
  28.        
  29.         return result;
  30.     }
  31.  
  32.     public static void main(String[] args) {
  33.         System.out.println("Wynik operacji 2 + 2: " + evaluate("2 + 2"));
  34.         System.out.println("Wynik operacji 5 - 3: " + evaluate("5 - 3"));
  35.         System.out.println("Wynik operacji 8 * 2: " + evaluate("8 * 2"));
  36.         System.out.println("Wynik operacji 20 / 4: " + evaluate("20 / 4"));
  37.     }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top