-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
49 lines (39 loc) · 1.3 KB
/
Copy pathCalculator.java
File metadata and controls
49 lines (39 loc) · 1.3 KB
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
42
43
44
45
46
47
48
49
// package com.package;
import java.util.Scanner;
class Calculator
{
static double result;
public static void main(String[] args)
{
Scanner reader=new Scanner(System.in);
System.out.println("Enter two Numbers: ");
// nextDouble() reads the next double from the keyb
double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.println("Enter an operator (+,-,*,/) :");
char operator = reader.next().charAt(0);
// double result;
// switch case for each of the operations
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 an case constant (+,-,*,/)
default:
System.out.println("Error! operator is not correct");
// return 0;
}
//printing the result of the operations
System.out.println("%.1f %c %.1f= %.1f", first, operator, second, result );
}
}