+ 1
How to write a program that simulates a calculator in Java?
Write a program that gets the user to input three integer numbers. Then, input two operands. The sequence of the operand should follow this order, *, /, %, +, -. Sample Input Enter three integer numbers: 30 5 7 Enter two operands: + * Sample Output 30+5*7=65 My attempt is by using the if and else if method. There are so many conditions here so I used the else if (condition) for 24 times. This makes my code to be very long. Any other faster methods? Can somebody help me please? Thank you!
1 Answer
+ 3
First store those three numbers in an array. Then check for the priority of the operands(like assigning them some values i.e, "-" = 1, "+" = 2,... "*" = 5).
Then if the second priority is greater than the first priority, calculate the second and third elements of the array, then with the first one.
If the first priority is greater than the second priority, calculate the first two elements of the array then with the last one.