-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCalculator.java
More file actions
43 lines (40 loc) · 1.32 KB
/
Copy pathSimpleCalculator.java
File metadata and controls
43 lines (40 loc) · 1.32 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
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("---JAVA CALCULATOR---");
boolean flag= true;
while(flag){
System.out.print("Start/Exit: ");
String calculator= sc.next();
if(calculator.equals("start") || calculator.equals("Start")){
System.out.print("Calculate: ");
double n1= sc.nextInt();
char op= sc.next().charAt(0);
double n2= sc.nextInt();
double result;
if(op=='+' || op=='-' || op=='*' || op=='/'){
if(op=='+'){
result= n1 + n2;
}
else if(op=='-'){
result= n1-n2;
}
else if(op=='*'){
result=n1*n2;
}
else{
result= n1/n2;
}
System.out.println(result);
}
else{
System.out.println("Invalid operation");
}
}
else if(calculator.equals("exit")){
flag= false;
}
}
}
}