-
Notifications
You must be signed in to change notification settings - Fork 13
/
JavaCalculator.java
41 lines (37 loc) · 1.31 KB
/
JavaCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
public class JavaCalculator
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter two numbers: ");
// nextDouble() reads the next double from the keyboard
double first = in.nextDouble();
double second = in.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
//Char is a single alphabet where as String is a sequence of characters.
//char is a primitive data type
char operator = in.next().charAt(0); //This will return the first char of the string
double result;
switch(operator)
{
case '+':
result = first + second;
//break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
// operator doesn't match any case constant (+, -, *, /)
default:
System.out.printf("Error! operator is not correct");
//return;
}
System.out.printf("Number1 \t"+ first + "\t "+ operator + "\tNumber 2 \t"+ second + "=\t"+ result);
}
}