I am quite new learner of java se, and i am coming to some problems.
The code below should be a calculator. It works fine But. To calculate the output the input of the scanner class should be entered like this:
1: enter 1st number - hit enter key on the keyboard
2: enter the case +-* or / then again hit the enter in keyboard
3: enter the second number and again hit the enter key
4: Finally comes the output
so it looks like this
Calculator
2
-
1
Calculation is 1:
The question is is there a way to put it in a single row
1. Enter the both numbers 2-1 - hit the enter on the keyboard
2. The output shows like Calculation: 1
public class Calculator {
public static void main(String... args) {
int num1;
int num2;
Scanner scan = new Scanner(System.in);
System.out.println("Calculator");
num1 = scan.nextInt();
String str = scan.next();
num2 = scan.nextInt();
System.out.print(("Calculation is " + calculate(num1, str, num2)));
scan.close();
}
private static int calculate(int num1, String str, int num2) {
switch (str.charAt(0)) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
}
return calculate(num1, str, num2);
}
}