Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.function.IntBinaryOperator;
- public class LambdaCalc
- {
- static IntBinaryOperator add = (a, b) -> a + b;
- static IntBinaryOperator sub = (a, b) -> a - b;
- static IntBinaryOperator mult = (a, b) -> a * b;
- public static int calculate( int a, int b, IntBinaryOperator op )
- {
- return op.applyAsInt(a, b);
- }
- public static void printCalc( String sign, int a, int b, IntBinaryOperator op )
- {
- System.out.println(a + " " + sign + " " + b + " = " + calculate(a, b, op));
- }
- public static void main( String[] args )
- {
- int a, b;
- Scanner input = new Scanner(System.in);
- System.out.print("Please enter two integers: ");
- a = input.nextInt();
- b = input.nextInt();
- printCalc("+", a, b, add);
- printCalc("-", a, b, sub);
- printCalc("*", a, b, mult);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment