-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1 v.java
More file actions
46 lines (42 loc) · 1.44 KB
/
task1 v.java
File metadata and controls
46 lines (42 loc) · 1.44 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
//a calculator to help user perform the basic operations (+, -, * and /).
package calculator;
//importing scanner
import java.util.Scanner;
public class Calculator {
static char operator;
static Double num1,num2,total;
public static void main(String[] args) {
//creating scanner class
Scanner input = new Scanner(System.in);
//ask user to enter operator
System.out.println("enter an operator");
operator=input.next().charAt(operator);
//ask user to enter num 1
System.out.println("enter num1");
num1=input.nextDouble();
//ask user to enter num 2
System.out.println("enter num2");
num2=input.nextDouble();
switch (operator){
//addition
case'+':
total=num1+num2;
System.out.println(num1+"+"+num2+"="+total);
break;
//subtraction
case'-':
total=num1-num2;
System.out.println(num1+"-"+num2+"="+total);
break;
//multiplication
case'*':
total=num1*num2;
System.out.println(num1+"*"+num2+"="+total);
break;
//division
case'/':
total=num1/num2;
System.out.println(num1+"/"+num2+"="+total);
}
}
}