-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
48 lines (48 loc) · 1.54 KB
/
Copy pathCalculator.java
File metadata and controls
48 lines (48 loc) · 1.54 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
import java.util.*;
public class Calculator {
public static void main(String[] args) {
//this is a calculator
Scanner sc=new Scanner(System.in);
System.out.println("\nWelcome to the calculator program!\nplease press 'x' as the input for operator to stop the calculator");
char op='+';
while(op!='x'){
System.out.println("\nenter the 1st number: ");
int a=sc.nextInt();
System.out.println("enter the operation you want to perform: ");
op=sc.next().charAt(0);
System.out.println("enter 2nd number ");
int b=sc.nextInt();
switch(op){
case '+':
{
System.out.println("ths answer is: "+(a+b));
break;
}
case '-':
{
System.out.println("the answer is: "+(a-b));
break;
}
case '*':
{
System.out.println("the answer is: "+(a*b));
break;
}
case '/':
{
System.out.println("the answer is:" +(a/b));
break;
}
case 'x':
{
System.out.println("all operastins ended, calculator has been off");
break;
}
default:
System.out.println("invalid operator please try again...");
}
if(op!='x')
System.out.println("\nnext operation");
}
}
}